gpt4 book ai didi

c++ - 类类型是否会发生左值到右值的转换?

转载 作者:行者123 更新时间:2023-12-01 12:46:27 27 4
gpt4 key购买 nike

我在网上看到的几乎每个左值到右值转换的例子都与基本类型有关,比如 int等等。
我自己找不到适用于类类型的 l2r 示例;在所有看似适用的例子中,通常有一个函数涉及 lvalue-ref(如 copy-ctor),为此 l2r 似乎被抑制(参见例如 this 问题)。
然而,在 l2r 本身的描述中,有一个关于类类型的子句(来自 [conv.lval]):

the result of the conversion is determined according to the following rules:

<...> if T has a class type, the conversion copy-initializes a temporary of type T from the glvalue and the result of the conversion is a prvalue for the temporary.


有人可以举一个这个条款的例子吗?我不能。

最佳答案

一个例子是丢弃值表达式中的 volatile 对象:

struct A {};

void f()
{
volatile A a;
a;
}

根据 [expr.context]/2 :

In some contexts, an expression only appears for its side effects. Such an expression is called a discarded-value expression. The array-to-pointer and function-to-pointer standard conversions are not applied. The lvalue-to-rvalue conversion is applied if and only if the expression is a glvalue of volatile-qualified type and it is one of the following:

  • ...
  • id-expression,
  • ...


左值到右值的转换应用于 a .

关于c++ - 类类型是否会发生左值到右值的转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60984469/

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