Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

lvalue-to-rvalue conversion applied to class types

55 views
Skip to first unread message

J.A. Belloc

unread,
Dec 7, 2014, 6:03:27 PM12/7/14
to
Consider the following snippet:

struct A{ int i; A(int j): i(j){} };
int main()
{
A a(1), b(2);
b = a;
}

The expression `b = a;` doesn't apply the lvalue-to-rvalue conversion to `a` because b.operator=(a) is invoked by the compiler and there is no need to invoke the alluded conversion, as the lvalue `a` binds to the argument (const A&) of the assignment operator. Also, AFAICT this happens for every operator, whose operand is a class type. Thus, I'd like to see, if possible, one single example of the lvalue-to-rvalue conversion being applied to a class type. Otherwise, I ask, why does §4.1/1 in the Standard refer to this conversion?

Öö Tiib

unread,
Dec 7, 2014, 8:05:48 PM12/7/14
to
Maybe I misunderstand what you are asking.
Ok, assignment accepts the parameter by reference to const so no conversion
is needed. However for example if some other function (or operator
or the like callable) accepts a parameter by value but lvalue is given
as argument in call then there is lvalue-to-rvalue conversion applied to
it.

J.A. Belloc

unread,
Dec 8, 2014, 7:04:26 AM12/8/14
to
No there isn't, as the copy constructor is invoked and again, an lvalue binds to the constructor's parameter. No need for an lvaue-to-rvalue conversion.

J.A. Belloc

unread,
Dec 8, 2014, 8:36:04 AM12/8/14
to
I'm not sure, but I may have found one example where the lvalue-to-rvalue conversion occurs for a class type. See the snippet below:

class A{};
int main()
{
A a, b;
a = std::move(b);
}

std::move(b) is an xvalue. In order for this xvalue to be used to update the object `a`, according to §3.10/2 ("Whenever a glvalue appears in a context where a prvalue is expected, the glvalue is converted to a prvalue"), it seems like this xvalue is converted to a prvalue.

I would very much appreciate comments on this. Other examples are also welcome. Thanks.

Öö Tiib

unread,
Dec 8, 2014, 10:08:47 AM12/8/14
to
Standard does claim exactly same about that lvalue-to-rvalue
conversion of class type: "this conversion effectively copy-constructs
a temporary object of type T using the original glvalue as the
constructor argument, and that temporary object is returned as a
prvalue." So can you please clarify what you mean by "no" and how it
differs from behavior described by standard?

J.A. Belloc

unread,
Dec 8, 2014, 1:08:49 PM12/8/14
to
The Standard doesn't say "copy-constructs", but "copy-initializes", which is different from a copy construction. A copy-initialization, as you probably know, initializes an object, possibly using some kind of conversion (standard conversion, user-defined conversion, or a standard conversion sequence) followed by an implicit copy construction of the temporary, when this copy is not elided. These are the exact words in paragraph §4.1/2, second bullet point, of N3797:

"- Otherwise, 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."

But the important point here was that, while trying to write down this answer, I noticed that paragraphs §4.1/1 and §4.1/2 were modified from C++11 (N3337) to C++14 (N3797). When I posted my question I was thinking in terms of C++11 (N3337), which doesn't show the bullet point above. So that, now reading the new version of paragraph §4.1/2, I can see that the Standard explicitly defines a copy initialization of a class type, as an lvalue-to-rvalue conversion. Note that this definition doesn't exist in C++11 (N3337). Therefore, the revised version of §4.1/2 answers my original question. Thanks for your attention.
0 new messages