是否有一个模板元程序来确定编译器在编译时的端序
Is there a template-meta program to determine the endianness of the compiler at compile time?
可能重复:
有没有一种方法可以进行C++风格的编译时断言来确定machine';s endianness?
我正在寻找一个本着Boost::type_traits精神的模板元程序,它将返回编译器是big还是little endian。类似is_big_endian<T>
。我该怎么写?
它的用途是创建一个库,通过实现基于端序的特定模板专门化,该库将自动适应环境。例如,
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];
}
这将允许is_big_endian作为模板参数传递。
有一个Boost头文件,它定义了一个可以使用的宏:Boost/detail/endian.hpp。不需要使用模板元编程。
如果使用gcc(或clang),则可以使用预处理器变量__BYTE_ORDER__:
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
// little endian stuff
#else
// big endian stuff
#endif // __BYTE_ORDER__
相关文章:
- 基于编译器选项的编译二进制路径
- 编译器不会使用 -std=c++11 编译智能指针
- 编译"运算符删除"时C++编译器如何工作?
- 无法使用 aarch64-unknown-nto-qnx7.0.0-g++-5.4.0 编译器交叉编译 QNX 平台的
- 编译器现在遵循C++14标准,我的项目不再编译神秘的SFML错误
- C++使用现代编译器编译的项目,但链接到过时的libstdc++
- 交叉编译:特殊的交叉编译器还是只是带有选项 -march 的 gcc?
- 如何编译使用从不同编译器编译的库的应用程序?
- 如何使用MinGW编译器和SSL支持静态编译Qt
- Arduino-Makefile:编译器无法编译用户库 cpp 文件
- 编译器如何在编译时执行数据绑定?
- 如何使用CLion和MSVC编译器工具链编译wxWidgets Hello World应用程序而没有错误?
- 使用Bison生成的编译器编译源代码
- CMake + Qt,moc 编译失败,无法实现 QMetaObject 方法(编译器找不到基本 ui 对象的标头?
- C++程序编译器在使用g++命令编译后自动运行exe文件
- boost::spirit--试图编译大多数简单代码的编译器错误
- 使用其他编译器编译QT代码
- 如何在cmake工具链文件中设置编译功能,以便已知的自定义编译器使用target_compile_features
- C++编译器能在编译时计算出文字的除法结果吗
- 如何使用 g++ 编译器和 Eigen 库在 Windows 上进行编译