C++ sizeof 运算符的返回类型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19870192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the return type of sizeof operator?
提问by Coder777
What is the return type of sizeof operator? cppreference.com & msdn says sizeof returns size_t. Does it really return a size_t? I'm using VS2010 Professional, and targeting for x64.
sizeof 运算符的返回类型是什么?cppreference.com 和 msdn 说 sizeof 返回 size_t。它真的返回 size_t 吗?我正在使用 VS2010 Professional,并针对 x64。
int main()
{
int size = sizeof(int); // No warning
int length = strlen("Expo"); //warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
return 0;
}
I have this question because first line is not issuing any warning, whereas the second does. Even if I change it to char size, I don't get any warnings.
我有这个问题,因为第一行没有发出任何警告,而第二行发出任何警告。即使我将其更改为字符大小,也不会收到任何警告。
回答by Matteo Italia
C++11, §5.3.3 ?6
C++11, §5.3.3 ?6
The result of
sizeof
andsizeof...
is a constant of typestd::size_t
. [ Note: std::size_t is defined in the standard header (18.2). — end note ]
sizeof
and的结果sizeof...
是一个类型为 的常量std::size_t
。[ 注意:std::size_t 在标准头文件 (18.2) 中定义。— 尾注 ]
You can also do a quick check:
您还可以进行快速检查:
#include <iostream>
#include <typeinfo>
#include <cstdlib>
int main()
{
std::cout<<(typeid(sizeof(int))==typeid(std::size_t))<<std::endl;
return 0;
}
which correctly outputs 1
on my machine.
1
在我的机器上正确输出。
As @Adam D. Ruppesaid in the comment, probably the compiler does not complain because, since it already knows the result, it knows that such "conversion" is not dangerous
正如@Adam D. Ruppe在评论中所说,编译器可能不会抱怨,因为它已经知道结果,它知道这种“转换”并不危险
回答by Vlad from Moscow
size_t is an alias of some implementation-defined unsigned integral type. In C++ opposite to C where sizeof operator may be applied to VLA arrays the operand of sizeof operator is not evaluated (at run time). It is a constant. If the value of sizeof operator can be fit into int type the compiler does not issue a warning. In the second example std::strlen is evaluated at run time so its result can do not fit into int so the compiler issues a warning. You could substitute std:;strlen with your own constexpr function (some recursive function). In this case if the result can fit into int I think that the compiler will not issue a warning.
size_t 是某些实现定义的无符号整数类型的别名。在与 C 相对的 C++ 中,sizeof 运算符可以应用于 VLA 数组,sizeof 运算符的操作数不会被计算(在运行时)。它是一个常数。如果 sizeof 运算符的值可以适合 int 类型,则编译器不会发出警告。在第二个示例中, std::strlen 在运行时进行评估,因此其结果无法放入 int 中,因此编译器发出警告。你可以用你自己的 constexpr 函数(一些递归函数)替换 std:;strlen 。在这种情况下,如果结果可以放入 int 我认为编译器不会发出警告。
回答by dpossamai
The sizeof operator is used to get the size of types or variable in bytes. Returns an unsigned integer type of at least 16 bit. It's used to get portability.
sizeof 运算符用于以字节为单位获取类型或变量的大小。返回至少 16 位的无符号整数类型。它用于获得便携性。
This warning is because of the unsigned integer where is defined the size_t.
这个警告是因为定义了 size_t 的无符号整数。