Is there a template meta program for determining the finiteness of the compiler at compile time?

Possible duplicate:
Is there a way to make a C ++ style compile time statement to determine the limb of a machine?

I am looking for a Boost :: type_traits template meta program that will return whether the compiler is large or small. Something like is_big_endian<T> . How to write this?

Using this method is to create a library that automatically adapts to the environment, by implementing a specific specialized specialization based on the final information. For instance,

 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 passing is_big_endian as a template argument.

+4
source share
2 answers

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

+5
source

if you use gcc (or clang), you can use the __ BYTE_ORDER __ preprocessor variable:

 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ // little endian stuff #else // big endian stuff #endif // __BYTE_ORDER__ 
+2
source

All Articles