class 类型是否发生过左值到右值的转换?
Does lvalue-to-rvalue conversion ever happen to class types?
实际上,我在网上看到的每个左值到右值转换的示例都与基本类型有关,例如 int
等
我自己找不到适用于 class 类型的 l2r 示例;在所有看似适用的示例中,通常有一个函数 imvolved 采用 lvalue-ref(如 copy-ctor),为此 l2r 似乎被抑制(参见例如 问题)。
但是在 l2r 本身的描述中有一个关于 class 类型的子句(来自 [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.
有人可以举出这个条款的例子吗?我不能。
一个示例是丢弃值表达式中的可变对象:
struct A {};
void f()
{
volatile A a;
a;
}
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
。
实际上,我在网上看到的每个左值到右值转换的示例都与基本类型有关,例如 int
等
我自己找不到适用于 class 类型的 l2r 示例;在所有看似适用的示例中,通常有一个函数 imvolved 采用 lvalue-ref(如 copy-ctor),为此 l2r 似乎被抑制(参见例如
但是在 l2r 本身的描述中有一个关于 class 类型的子句(来自 [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.
有人可以举出这个条款的例子吗?我不能。
一个示例是丢弃值表达式中的可变对象:
struct A {};
void f()
{
volatile A a;
a;
}
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
。