gpt4 book ai didi

c++ - 移位运算符、位运算符和 sizeof 运算符产生的结果的值类别是什么?

转载 作者:太空狗 更新时间:2023-10-29 23:20:59 25 4
gpt4 key购买 nike

移位运算符:<< >>按位运算符:~ , & , ^ , |sizeof 运算符:sizeof()

根据 C++ 标准 (n3797),我只能确认 ~产生纯右值 (5.3.1/2),但不产生上述其他值。

最佳答案

据我所知,结果是 prvalues 但这只是推测。这似乎以类似于 What is the value category of the operands of C++ operators when unspecified? 中涵盖的操作数的值类别的方式未指定。和 Does the standard mandate an lvalue-to-rvalue conversion of the pointer variable when applying indirection? .

3.10部分可以看出左值和右值有如下注释:

The discussion of each built-in operator in Clause 5 indicates the category of the value it yields and the value categories of the operands it expects.

但我们可以看到 5 部分仅在少数情况下明确说明值(value)类别。在这个特定问题的情况下,只明确说明了 & 结果的值类别~ .

即使未指定,我们也可以找到指向一致方向的线索。我们可以论证 >> 的操作数, << , ^|被转换为纯右值。这并不规定结果的值类别,但它似乎确实排除了根据第 5 节的 xvalue 结果。 7 段具有以下注释:

An expression is an xvalue if it is:

— the result of calling a function, whether implicitly or explicitly, whose return type is an rvalue reference to object type,

— a cast to an rvalue reference to object type,

— a class member access expression designating a non-static data member of non-reference type in which the object expression is an xvalue, or

— a .* pointer-to-member expression in which the first operand is an xvalue and the second operand is a pointer to data member.

In general, the effect of this rule is that named rvalue references are treated as lvalues and unnamed rvalue references to objects are treated as xvalues; rvalue references to functions are treated as lvalues whether named or not.

我没有看到任何合理的论据表明结果可能是一个左值,所以基本上留给我们的是一个纯右值

所以具体情况如下:

两者都是 ~&包含在 5.3.1 节中一元运算符 2 段说:

The result of each of the following unary operators is a prvalue.

移位运算符需要整数提升,这在 5.8 节中有所介绍移位运算符 1 段说:

The operands shall be of integral or unscoped enumeration type and integral promotions are performed.

我们可以看到积分提升需要来自 4.5 部分的 prvalues Integral promotions 在每个段落中都以以下内容开头:

A prvalue of [...]

两者都是 ^|需要通常的算术转换并且都说:

operator applies only to integral or unscoped enumeration operands

因此通常的算术转换的最后一个子句适用,它说:

Otherwise, the integral promotions (4.5) shall be performed on both operands.59

经验方法

Luc Danton 在他对 Empirically determine value category of C++11 expression? 的回答中采用了一种经验方法来确定表达式的值类别.该方法使用以下代码:

template<typename T>
struct value_category {
// Or can be an integral or enum value
static constexpr auto value = "prvalue";
};

template<typename T>
struct value_category<T&> {
static constexpr auto value = "lvalue";
};

template<typename T>
struct value_category<T&&> {
static constexpr auto value = "xvalue";
};

// Double parens for ensuring we inspect an expression,
// not an entity
#define VALUE_CATEGORY(expr) value_category<decltype((expr))>::value

答案概述了如下逻辑:

an lvalue expression results in an lvalue reference type, an xvalue in an rvalue reference type, and a prvalue in just the type.

以下示例均产生prvalue ( see it live ):

int x = 10, y = 2 ;
int &xr = x ;
int &yr = y ;

std::cout << VALUE_CATEGORY( x << y ) << std::endl ;
std::cout << VALUE_CATEGORY( 10 << 2 ) << std::endl ;
std::cout << VALUE_CATEGORY( xr << yr ) << std::endl ;

std::cout << VALUE_CATEGORY( x | y ) << std::endl ;
std::cout << VALUE_CATEGORY( 10 | 2 ) << std::endl ;

std::cout << VALUE_CATEGORY( x ^ y ) << std::endl ;
std::cout << VALUE_CATEGORY( 10 ^ 2 ) << std::endl ;

std::cout << VALUE_CATEGORY( sizeof( int ) ) << std::endl ;
std::cout << VALUE_CATEGORY( sizeof( x ) ) << std::endl ;

关于c++ - 移位运算符、位运算符和 sizeof 运算符产生的结果的值类别是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24518517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com