By fredoverflow


2010-06-22 23:03:48 8 Comments

I don't understand the last line of the example on page 148 of the FCD (§7.6.1.2/4):

const int&& foo();
int i;
struct A { double x; };
const A* a = new A();
decltype(foo()) x1 = i;     // type is const int&&
decltype(i) x2;             // type is int
decltype(a->x) x3;          // type is double
decltype((a->x)) x4 = x3;   // type is const double&

Why do the parentheses make a difference here? Shouldn't it simply be double like in the line above?

3 comments

@James McNellis 2010-06-22 23:12:13

decltype(a->x)

This gives you the type of the member variable A::x, which is double.

decltype((a->x))

This gives you the type of the expression (a->x), which is an lvalue expression (hence why it is a const reference--a is a const A*).

@fredoverflow 2010-06-23 09:51:07

Okay, I understand how the rules can be applied here now, but why are the rules like that? Why does it make sense to distinguish between a->x and (a->x)? It seems so random to me. Why would I ever want that behavior? Any ideas?

@fredoverflow 2010-08-30 19:21:21

Thanks, but both decltype(f()) and decltype ((f())) yield int on my system. Did I misunderstand you?

@James McNellis 2010-08-31 02:17:00

@Fred: Nevermind. I was wrong. In that particular case, the parentheses are ignored, so both should be const int.

@Greg Domjan 2010-06-22 23:10:58

The added parens are turning it into a lvalue.

MSDN says
The inner parentheses cause the statement to be evaluated as an expression instead of a member access. And because a is declared as a const pointer, the type is a reference to const double.

@Cubbi 2010-06-22 23:10:24

Just above that example, it says

  • if e is an unparenthesized id-expression or a class member access (5.2.5), decltype(e) is the type of the entity named by e.
  • if e is an lvalue, decltype(e) is T&, where T is the type of e;

I think decltype(a->x) is an example of the "class member access" and decltype((a->x)) is an example of lvalue.

@fredoverflow 2010-06-22 23:11:27

But that does not explain the const :)

@Cubbi 2010-06-22 23:12:30

@FredOverflow: Does too: a has type const A*

@fredoverflow 2010-06-22 23:17:35

How did I not see the const??? Thanks :)

@camino 2015-12-14 15:25:06

"class member access" should be "class data member access"

Related Questions

Sponsored Content

10 Answered Questions

2 Answered Questions

5 Answered Questions

1 Answered Questions

[SOLVED] decltype() of captured variable in lambda: GCC bug and/or Clang bug?

1 Answered Questions

[SOLVED] What are the type deduction rules of decltype(rvalue expr) ?

2 Answered Questions

[SOLVED] Compiler error while defining array using typeof and decltype

2 Answered Questions

[SOLVED] decltype(*this) bug in VS2013?

1 Answered Questions

[SOLVED] Why does scope resolution fail in presence of decltype?

  • 2013-07-06 03:32:46
  • Borgleader
  • 757 View
  • 15 Score
  • 1 Answer
  • Tags:   c++ c++11 decltype

2 Answered Questions

[SOLVED] What is decltype with two arguments?

3 Answered Questions

[SOLVED] Why is decltype not allowed on private member variables?

  • 2012-06-07 00:37:09
  • karadoc
  • 3336 View
  • 12 Score
  • 3 Answer
  • Tags:   c++ c++11 decltype

Sponsored Content