Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a template-meta program to determine the endianness of the compiler at compile time? [duplicate]

Tags:

c++

c

boost

Possible Duplicate:
Is there a way to do a C++ style compile-time assertion to determine machine's endianness?

I am looking for a template meta program in the spirit of Boost::type_traits that would return whether the compiler is big or little endian. Something like is_big_endian<T>. How do I write this?

The use of this is to create a library that will automatically adapt itself to the environment, by implementing specific template specialization based on the endian-ness. For example,

template<>
void copy_big_endian_impl<true>(T *dst, const T *src, size_t sz) {
         // since already big endian, we just copy
         memcpy(dst, src, sz*sizeof(T));
}
template<>
void copy_big_endian_impl<false>(T *dst, const T *src, size_t sz) {
         for (int idx=0; idx<sz; idx++)
             dst[idx] = flip(src[idx];
}

This would allow is_big_endian to be passed as a template argument.

like image 202
sep Avatar asked Jun 01 '11 01:06

sep


Video Answer


1 Answers

There is a Boost header file that defines a macro you can use: boost/detail/endian.hpp. There's no need to resort to template metaprogramming.

like image 191
Seth Johnson Avatar answered Nov 02 '22 09:11

Seth Johnson