0

Let's consider very short piece of code in C++:

class T{
};
T *t = new T();

What is type of *t? rvalue/lvalue/xvalue/glvalue? why? Thanks in advance.

Gilgamesz
  • 4,727
  • 3
  • 28
  • 63

1 Answers1

1

The check for lvalue is very simple. Can it appear on the left hand side of an assignment operator?

In your case, the answer is "yes".

*t = T();

is valid. Hence, the value of type of *t is lvalue.

By virtue of being an lvalue, it is also a glvalue.

From https://timsong-cpp.github.io/cppwp/n3337/basic.lval:

A glvalue (“generalized” lvalue) is an lvalue or an xvalue.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    It's also a glvalue. – Mooing Duck May 23 '17 at 18:51
  • 1
    Well, overloading `operator =` for `T` would make it able to be called with an rvalue, too, so this check is not really accurate. – Quentin May 23 '17 at 19:21
  • @Quentin, true. I am sure the OP is not looking at anything that deep. – R Sahu May 23 '17 at 19:31
  • 1
    @RSahu I wrote too fast. It actually does that even without overloading (which, in restrospect, is obvious)... So `struct Foo { }; Foo{} = Foo{};` compiles just fine. – Quentin May 23 '17 at 19:37
  • @Quentin, by strict definition of the standards, an lvalue *designates a function or an object*. It doesn't say anything about the life time of the object. If we go by that imprecise definition, even a temporary object, which does designate an object, should qualify as an lvalue. – R Sahu May 23 '17 at 19:45