sizeof是一个< em >编译时操作符< / em >,所以在编译时sizeof及其操作数将被结果值替换。的操作数为not evaluate (除非它是一个变长数组);只有结果的type才重要。
short func(short x) { // this function never gets called !!
printf("%d", x); // this print never happens
return x;
}
int main() {
printf("%d", sizeof(func(3))); // all that matters to sizeof is the
// return type of the function.
return 0;
}
struct A
{
A(); //no definition, which means we cannot create instance!
int f(); //no definition, which means we cannot call it
};
int main() {
std::cout << sizeof(A().f())<< std::endl;
return 0;
}