Re: Two Templates
On Oct 23, 3:16 pm, Ian Collins <ian-n...@hotmail.com> wrote:
On 10/24/10 10:01 AM, KevinSimonson wrote:
Can someone tell me what the following code does? The first
"template<typename T, Endianess endianPolicy> struct TransformTo"
makes sense to me, but I don't understand what the "template<typename
T> struct TransformTo< T, Machine::endianess>" does. Also, in =
the
first one "endianPolicy" is mentioned but never used. So does that
mean including it has no effect? I'm kind of confused on that.
As the comment says...
//! \brief Generic function object to give its char serialization a
given
//! specified byte ordering.
//!
//! The byte ordering of the argument is swapped unless it matches the
byte
//! ordering of the target machine.
//! We use partial specialization to achieve this.
the second is a partial specialisation of the first.
template<typename T, Endianess endianPolicy> struct TransformTo
{
T operator()(T value) const { return swapEndianess< T>( value=
); }
};
template<typename T> struct TransformTo< T, Machine::endianess>
{
T operator()(T value) const { return value; }
};
The second template argument is there to differentiate between the
general case and the specialised one.
--
Ian Collins
So are you saying that if the "endianess" of the host machine is the
same as the "endianess" of the target machine, "TransformTo( value)"
will just return the value of "value", but that if the "endianess" of
the host machine is _different_ from the "endianess" of the target
machine, the bytes will get swapped? Presuming that "swapEndianess()"
swaps the bytes?
Kevin S