7

Practically every example of lvalue-to-rvalue conversion I've seen on the web relates to fundamental types like int etc.

I couldn't find an example of l2r applicable to class types myself; in all the seemingly applicable examples there's usually a function involved that takes lvalue-ref (like copy-ctor), for which l2r seems to be suppressed (see e.g. this question).

However in the description of l2r itself there's a clause about class types (from [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.

Could someone give an example of this clause? I can't.

ledonter
  • 1,269
  • 9
  • 27
  • There is basic.lval/3 "Note: Whenever a glvalue appears in a context where a prvalue is expected, the glvalue is converted to a prvalue". Something tickles my mind that there are such situations where the standard specifies a prvalue of class type as being expected – M.M Apr 02 '20 at 05:21

1 Answers1

5

An example is volatile objects in discarded-value expressions:

struct A {};

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

According to [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,
  • ...

Lvalue-to-rvalue conversion is applied to a.

xskxzr
  • 12,442
  • 12
  • 37
  • 77
  • So in that example, is there a copy-initialization of a temporary happening from `a`? Like copy-ctors called etc. I'm mostly interested in copy-initialization part of the clause; or is it elided somehow maybe?.. – ledonter Apr 02 '20 at 06:22
  • 1
    @ledonter I think there indeed is. – xskxzr Apr 02 '20 at 06:23
  • Hmm ... doesn't seem to be the case with [GCC and Clang](https://godbolt.org/z/K4jhce). In fact, Clang says `warning: expression result unused; assign into a variable to force a volatile load [-Wunused-volatile-lvalue]` – L. F. Apr 02 '20 at 06:27
  • 2
    @L.F. There is a [bug report](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84699). – xskxzr Apr 02 '20 at 06:37