我有点迷路了。
在我的代码中,我需要一个 unsigned int
作为 SAP Netweaver 调用 Rfc 函数的引用,我为函数提供的缓冲区大小。
在我的 VS 环境中,我编写了这个:
auto buffer_length = sizeof(buffer);
auto rc = RfcCall...(..., &buffer_length, ...)
这编译得很好,悬停在它显示的“buffer_length”上,它确实是一个无符号整数。
使用 GCC 4.8.5 编译的完全相同的代码会抛出编译器错误 - 原来我的变量是 int 类型。
但是为什么?我该怎么做才能防止这种情况发生? 我认为有一些标准需要遵守... :(
最佳答案
根据 §18.2p6 [support.types]:
The type
size_t
is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.
和§5.3.3p6 [expr.sizeof]:
The result of
sizeof
andsizeof...
is a constant of typestd::size_t
.
因此,根据标准,sizeof(buffer)
是一个无符号 整数类型。要么 GCC 中存在错误(这不太可能,如 std::size_t
is unsigned long
),要么您的错误不在您认为的位置。
关于c++ - 从 sizeof 自动扣除的类型在 Visual Studio C++ 和 GCC 之间不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55168316/