c++ - 从 sizeof 自动扣除的类型在 Visual Studio C++ 和 GCC 之间不同

标签 c++ sizeof auto

我有点迷路了。

在我的代码中,我需要一个 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 and sizeof... is a constant of type std::size_t.

因此,根据标准,sizeof(buffer) 是一个无符号 整数类型。要么 GCC 中存在错误(这不太可能,如 std::size_t is unsigned long ),要么您的错误不在您认为的位置。

关于c++ - 从 sizeof 自动扣除的类型在 Visual Studio C++ 和 GCC 之间不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55168316/

相关文章:

c++ - 为什么 sizeof(intArray)/sizeof(int) 给出不同的值?

c++ - 带数组键的二进制映射 C++

C++ - 自动返回引用和非引用类型

c++ - 常量数组的自动引用

c++ - 隐式自动类型说明符

c++ - clang-tidy(版本 14)是否正确,此代码上有 `bugprone-use-after-move` 错误?

c++ - 是否可以在一个类中包含另一个类中的类对象数组?

c++ - 我怎样才能得到一个vector::value_type的sizeof?

c - 我如何计算没有。字符串数组中的字符串?

c++ - 数组 C++ 中每个 boolean 值 1 位