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.