简体   繁体   English

sizeof操作数得到评估?

[英]sizeof operands get evaluated?

AFAIK sizeof doesn't evaluate its operands it C++. AFAIK sizeof不会将其操作数评估为C ++。

Eg 例如

int x = 0;
sizeof(x += 1); // value of x is not changed

But what does this mean? 但是,这是什么意思?

int arr[5];
sizeof(arr+0); // here array is converted to pointer

Why does the arithmetic on array is applied here? 为什么在这里应用数组算术?

(§ 5.3.3/4) The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are not applied to the operand of sizeof. (第5.3.3 / 4节)左值到右值(4.1),数组到指针(4.2)和函数到指针(4.3)标准转换不应用于sizeof的操作数。

The sizeof() operator is calculated at compile time. sizeof()运算符在编译时计算。 The expressions are NOT evaluated. 表达式未被评估。 It is the type of the expression that is calculated (at compile time) and then used by sizeof(). 它是计算表达式的类型 (在编译时),然后由sizeof()使用。

So in your first one: 所以在你的第一个:

sizeof( x += 1);

The type of x is int. x的类型是int。 The result of the += operator is still int. + =运算符的结果仍然是int。 So the sizeof() is still the size of int. 所以sizeof()仍然是int的大小。

In this: 在这:

sizeof(arr+0);

Here arr is an array and would have returned the size of the array (if used by itself). 这里arr是一个数组,并且会返回数组的大小(如果它本身使用的话)。 But the operator + causes the array to decay into a pointer. 但是运算符+使数组衰减成指针。 The result of the + operator on an array and an integer is a pointer. 数组上的+运算符和整数的结果是指针。 So here the sizeof() operator will return the sizeof a pointer. 所以这里sizeof()运算符将返回指针的大小。

(§ 5.3.3/4) The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are not applied to the operand of sizeof. (第5.3.3 / 4节)左值到右值(4.1),数组到指针(4.2)和函数到指针(4.3)标准转换不应用于sizeof的操作数。

This means that: 这意味着:

std::cout << sizeof(arr);
       // would print sizeof(int)* 5 (because there is no conversion)
       // if sizeof() had behaved like a normal function there
       // would have been a conversion but as you pointed out that
       // does not apply.

But here: 但在这里:

std::cout << sizeof(arr + 5);
       // prints the sizeof(int*) because the result of the expression
       // is a pointer type (int*)

As a side note: 作为旁注:

This is why 这就是为什么

int x[0];
int const xSize = sizeof(x)/sizeof(x[0]);

// This works correctly even though x[0] is technically
// not valid if used in a real expression (but is valid here).

It isn't. 事实并非如此。 In an arithmetic expression, array names decay into pointers. 在算术表达式中,数组名称会衰减为指针。 That says nothing about performing the calculation itself. 这对于执行计算本身没有任何意义。 The type of + is deducible from the types of its operands, in this case pointer and integer, yielding the same result as sizeof(int*) . +的类型可以从其操作数的类型中推导出来,在本例中为指针和整数,产生与sizeof(int*)相同的结果。

I don't know much about this, but if as you say, sizeof doesn't evaluate its operand then it must just rely on the type of the expression it's used on. 我不太了解这一点,但如果你说,sizeof不评估它的操作数,那么它必须只依赖于它所使用的表达式的类型 So I would guess that arr on its own has a different type to arr+0 , and that's why it was used. 所以我猜这个arr本身有一个与arr+0不同的类型,这就是它被使用的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM