-3

I know that if I apply decltype to p, where p is an int*, decltype(*p) is int&. And decltype(&p) is int**.

A reference being an rvalue, do I always get a pointer when applying decltype to an rvalue?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jake Wright
  • 373
  • 1
  • 8
  • 4
    What do you mean by "a reference being an rvalue" ? – M.M Jan 06 '18 at 22:56
  • That the address-of operator yields an rvalue. – Jake Wright Jan 06 '18 at 23:00
  • 2
    The address-of operator gives a pointer prvalue, not a reference ... and decltype of a pointer prvalue is indeed a pointer type, if that's what you are asking. decltype of prvalues that are not pointers, does not give a pointer type – M.M Jan 06 '18 at 23:01
  • 3
    A reference is not an rvalue. References have to do with *types*, and rvalues with *expressions*. I think you have a category error in your thinking. – Kerrek SB Jan 06 '18 at 23:03
  • Note also that "rvalue" isn't a value category, but rather a value category *group* consisting of the (actual) value categories xvalue and prvalue; `decltype` distinguishes those two. – Kerrek SB Jan 06 '18 at 23:09
  • @Ron: I'm actually stufying from C++ Primer at the moment and the question was asked as a means to try to understand the topic from the book. I've went trough the the previous chapters with no excessive trouble, but the Expresstions chapter hit me hard from the very fundamentals. Do you have any recommandations for well explained material on those online? – Jake Wright Jan 06 '18 at 23:29

1 Answers1

3

decltype applied to...

  • a prvalue expression gives a non-reference type,
  • an xvalue expression gives an rvalue reference type, and
  • an lvalue expression gives an lvalue reference type.

(There is an exception for id-expressions that I won't go into. Just consider "expression" to mean "any expression other than an id-expression" for the purpose of this answer.)

In all cases, the underlying type of the decltype type is the type of the expression.

To put this in code, let U be an object type. Then:

  • With U f();, decltype(f()) is U.
  • With U& f();, decltype(f()) is U&.
  • With U&& f();, decltype(f()) is U&&.

In your example, *p is an lvalue of type int, so decltype(*p) is int&, and &p is a prvalue of type int**, so decltype(&p) is int**.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084