C 和 C++ 中 sizeof 运算符的不同输出
Different outputs of sizeof operator in C and C++
C 和 C++ 中 sizeof()
运算符的不同输出。
C:
int main()
{
printf("%zu\n", sizeof(1 == 1));
return 0;
}
输出:
4
在 C++ 中:
int main()
{
std::cout << sizeof(1 == 1) << std::endl;
return 0;
}
输出:
1
问题:
- 为什么输出不同?
sizeof
是否独立于 OS 或编译器?
- 是否依赖于语言?
在 C
中 ==
和 !=
运算符的结果是 int
根据 N1570 草案 - 6.5.9 等式运算符
4
表示sizeof(int)
,但是要看架构。
在 C++
中 ==
和 !=
运算符的结果是 bool
根据 N4296 草案 - 5.10 等式运算符
1
表示sizeof(bool)
大小不能小于一个字节。但是大于一个字节是合法的。
因为 C 中的结果类型是 int
(4 个字节是典型的大小),而在 C++ 中它是 bool
(1 是典型的大小)。
这些值取决于实现。
这是一个 C11 程序,它演示了使用 _Generic
(典型输出 int 4
):
#include <stdio.h>
void what_int(){
printf("int %lu",sizeof(int));
}
void what_other(){
printf("other ?");
}
#define what(x) _Generic((x), \
int : what_int(),\
default: what_other()\
)
int main(void) {
what(1==1);
return 0;
}
这是一个 C++ 程序,演示了使用模板特化(典型输出 bool 1
):
#include <iostream>
template<typename T>
void what(T x){
std::cout<<"other "<<sizeof(T);
}
template<>
void what(bool x){
std::cout<<"bool "<<sizeof(bool);
}
int main(){
what(1==1);
return 0;
}
我想不出任何同时是 C 和 C++ 的代码会产生不同的结果。请接受这个挑战。
6.5.9 相等运算符
The ==
(equal to) and !=
(not equal to) operators are analogous to the relational operators except for their lower precedence. Each of the operators yields 1
if the specified relation is true and 0
if it is false. The result has type int
.
因此,sizeof(1 == 1)
将 return 等于 sizeof(int)
的值,这是 实现定义的 ,在您的情况下它是 4
.
5.10 等号运算符
The ==
(equal to) and the !=
(not equal to) operators group left-to-right. The operands shall have arithmetic, enumeration, pointer, or pointer to member type, or type std::nullptr_t
. The operators ==
and !=
both yield true
or false
, i.e., a result of type bool
.
因此,sizeof(1 == 1)
将 return 等于 sizeof(bool)
的值,这是 实现定义的 ,在您的情况下它是 1
.
C 和 C++ 中 sizeof()
运算符的不同输出。
C:
int main()
{
printf("%zu\n", sizeof(1 == 1));
return 0;
}
输出:
4
在 C++ 中:
int main()
{
std::cout << sizeof(1 == 1) << std::endl;
return 0;
}
输出:
1
问题:
- 为什么输出不同?
sizeof
是否独立于 OS 或编译器?- 是否依赖于语言?
在 C
中 ==
和 !=
运算符的结果是 int
根据 N1570 草案 - 6.5.9 等式运算符
4
表示sizeof(int)
,但是要看架构。
在 C++
中 ==
和 !=
运算符的结果是 bool
根据 N4296 草案 - 5.10 等式运算符
1
表示sizeof(bool)
大小不能小于一个字节。但是大于一个字节是合法的。
因为 C 中的结果类型是 int
(4 个字节是典型的大小),而在 C++ 中它是 bool
(1 是典型的大小)。
这些值取决于实现。
这是一个 C11 程序,它演示了使用 _Generic
(典型输出 int 4
):
#include <stdio.h>
void what_int(){
printf("int %lu",sizeof(int));
}
void what_other(){
printf("other ?");
}
#define what(x) _Generic((x), \
int : what_int(),\
default: what_other()\
)
int main(void) {
what(1==1);
return 0;
}
这是一个 C++ 程序,演示了使用模板特化(典型输出 bool 1
):
#include <iostream>
template<typename T>
void what(T x){
std::cout<<"other "<<sizeof(T);
}
template<>
void what(bool x){
std::cout<<"bool "<<sizeof(bool);
}
int main(){
what(1==1);
return 0;
}
我想不出任何同时是 C 和 C++ 的代码会产生不同的结果。请接受这个挑战。
6.5.9 相等运算符
The
==
(equal to) and!=
(not equal to) operators are analogous to the relational operators except for their lower precedence. Each of the operators yields1
if the specified relation is true and0
if it is false. The result has typeint
.
因此,sizeof(1 == 1)
将 return 等于 sizeof(int)
的值,这是 实现定义的 ,在您的情况下它是 4
.
5.10 等号运算符
The
==
(equal to) and the!=
(not equal to) operators group left-to-right. The operands shall have arithmetic, enumeration, pointer, or pointer to member type, or typestd::nullptr_t
. The operators==
and!=
both yieldtrue
orfalse
, i.e., a result of typebool
.
因此,sizeof(1 == 1)
将 return 等于 sizeof(bool)
的值,这是 实现定义的 ,在您的情况下它是 1
.