I've got more confused when I see this question: Is a class instantiation--class_name()
a xvalue or a prvalue? I'm trying to understand what does it mean by a class prvalue and a class xvalue. Someone tell me that they're called value category. But I think it would be better if I provide an example, because I'm very confused.
class myclass { public: myclass() {}; };
void myfunc(myclass c1){ }
int main(void)
{
myfunc(myclass());
}
So what's myclass()
? Is it a prvalue or xvalue?
I need a rule from the standard so that I wouldn't ask more questions.
The expression myclass()
is explicit type conversion using functional notation, per [expr.type.conv]/1:
A simple-type-specifier or typename-specifier followed by a parenthesized optional expression-list or by a braced-init-list (the initializer) constructs a value of the specified type given the initializer [..]
Here, the simple-type-specifier myclass
is followed by parenthesized expression-list ()
with an empty initializer-list. This expression constructs a value of type myclass
from the empty initializer-list.
So you might ask, Is that constructed value is prvalue or xvalue or lvalue. So here we've to invoke the immediately next paragraph: [expr.type.conv]/2:
If the initializer is a parenthesized single expression, the type conversion expression is equivalent to the corresponding cast expression. Otherwise, if the type is
cv void
and the initializer is()
or{}
(after pack expansion, if any), the expression is a prvalue of the specified type that performs no initialization. Otherwise, the expression is a prvalue of the specified type whose result object is direct-initialized with the initializer.
Our initializer is empty initializer-list, and the type is not cv void
, so we've ended with the sentence that says:
Otherwise, the expression is a prvalue of the specified type whose result object is direct-initialized with the initializer.
Hence, the expression myclass()
is a prvalue, whose result object is direct-initialized, from the empty initializer-list, by calling the default constructor.