Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables declared by &&

Thinking about (x|r|l|pr|gl)values, the following question came to my mind:

Consider the following two variable declarations:

X x = ...;

and

X&& x = ...;

and assume the ... do not deliver an xvalue.

Can anybody think of code not using decltype in which this makes a difference? In both cases, (x) will by an lvalue of type X, won't it?

like image 728
JohnB Avatar asked Dec 27 '15 10:12

JohnB


1 Answers

Maybe artificial example, but with

struct X
{
    X() = default;
    X(const X&) = delete;
    X operator =(const X&) = delete;
    X(X&&) = delete;
    X operator =(X&&) = delete;
};

X makeX() {return {};}

following compiles

X&& x = makeX();

whereas following doesn't

X x = makeX();
like image 94
Jarod42 Avatar answered Oct 04 '22 20:10

Jarod42