从OS X到ARM交叉编译时,以下程序无法编译:
#include <boost/cstdint.hpp>
#include <cstdint>
#include <type_traits>
static_assert(std::is_same<std::uint32_t, boost::uint32_t>::value, "");
int main() { }
我正在使用进行构建
arm-none-eabi-g++ -I /path/to/boost -std=c++11 -c main.cpp
其中
> arm-none-eabi-g++ --version
arm-none-eabi-g++ (GNU Tools for ARM Embedded Processors) 4.9.3 20150529 (release)
[ARM/embedded-4_9-branch revision 224288]
为了进一步诊断这个问题,我尝试了以下技巧:
template <typename T> struct show;
using A = show<std::uint32_t>::invalid;
using B = show<boost::uint32_t>::invalid;
编译器给我以下错误消息,指示std::uint32_t == long unsigned int
,而boost::uint32_t == unsigned int
:
main.cpp:8:32: error: 'invalid' in 'struct show<long unsigned int>' does not name a type
using A = show<std::uint32_t>::invalid;
^
main.cpp:9:34: error: 'invalid' in 'struct show<unsigned int>' does not name a type
using B = show<boost::uint32_t>::invalid;
我觉得这种情况非常令人惊讶。uint32_t
不应该总是代表完全相同的类型(32位宽度的无符号整数)吗?这是Boost中的一个错误还是我误解了什么?
uint32_t
应始终表示32位宽度的无符号整数,是。
但sizeof(long unsigned int) == sizeof(unsigned int)
完全有可能是真的。这是两种不同的类型,可以具有相同的宽度。
不能保证两个32位无符号整数值是相同的类型。
事实上,long
和int
可以是不同的类型,并且两者可以同时是32位值。对于wchar_t
和short
是16比特值,也是如此。