From: Nicola Musatti on

Gabriel Dos Reis wrote:
[...]
> The code shown used a static_cast, not a reinterpret_cast -- so in
> fact, it was restricting the space of allowed casts; it isn't "generic"
> if I may say. The question therefore is why the static_cast is better
> than simple
>
> *it = val;

Possibly because it ensures that no warnings are emitted for the cases
where the "castless" statement is legal.

Cheers,
Nicola Musatti


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Terry G on
Gabriel Dos Reis wrote:
> The code shown used a static_cast, not a reinterpret_cast -- so in
> fact, it was restricting the space of allowed casts; it isn't "generic"
> if I may say. The question therefore is why the static_cast is better
> than simple
>
> *it = val;

Ok, in my example (sorry I don't know how to link to another message),
I needed something like:

const uint8_t* start;
std::size_t len;
*it = T(start, start+len);

but I wanted the generic program to infer T from the iterator.
Here's the actual code from the library I'm creating.
This is a low-level routine, used to copy an array of fixed-size data
from a
message received over a wire.
Each data element is aligned on a 32-bit boundary (an external requirement).
Should I be doing something else, i.e. is my design fundamentally stupid?
In the past, I just provided a void* interface and everyone was happy.
Then I provided a vector<vector<uint8_t>> return value, which would copy
the
data (twice?).
Now, I'm trying to do better (more generic, more efficient), but I have the
feeling I'm off track here.
What should the interface be for this sort of low-level task, i.e. copying
bytes out of a received message?

terry

template <class T, class OutIter>
static OutIter CopyOut(OutIter dst, const uint8_t* src, std::size_t size,
unsigned count)
{
if (count == 0)
return dst;
*dst = T(src, src+size);
if (--count) {
std::size_t stride = 4*((size+3)/4);
do {
src += stride;
*++dst = T(src, src+size);
} while (--count);
}
return ++dst;
} // CopyOut



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Gabriel Dos Reis on
"Nicola Musatti" <nicola.musatti(a)gmail.com> writes:

| Gabriel Dos Reis wrote:
| [...]
| > The code shown used a static_cast, not a reinterpret_cast -- so in
| > fact, it was restricting the space of allowed casts; it isn't "generic"
| > if I may say. The question therefore is why the static_cast is better
| > than simple
| >
| > *it = val;
|
| Possibly because it ensures that no warnings are emitted for the cases
| where the "castless" statement is legal.

Such as?

--
Gabriel Dos Reis
gdr(a)integrable-solutions.net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Alberto Ganesh Barbati on
Nicola Musatti ha scritto:
> Gabriel Dos Reis wrote:
> [...]
>> The code shown used a static_cast, not a reinterpret_cast -- so in
>> fact, it was restricting the space of allowed casts; it isn't "generic"
>> if I may say. The question therefore is why the static_cast is better
>> than simple
>>
>> *it = val;
>
> Possibly because it ensures that no warnings are emitted for the cases
> where the "castless" statement is legal.

Which can be either good or bad. As the code is generic, you don't know
a priori whether the caller is aware of the conversion and thus can
safely ignore the warning or is actually making a mistake that would be
exposed by it.

Ganesh

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

First  |  Prev  | 
Pages: 1 2 3 4 5 6
Prev: The D Programming Language
Next: CRTP question