Search code examples
c++implicit-conversionlvalue-to-rvalue

Does lvalue-to-rvalue conversion ever happen to class types?


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.


Solution

  • 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.