2

Possible Duplicate:
decltype and parenthesis

I found this on wikipedia :

    auto c = 0;           // c has type int
    auto d = c;           // d has type int
    decltype(c) e;        // e has type int, the type of the entity named by c
    decltype((c)) f = c;  // f has type int&, because (c) is an lvalue

And using ideone compiler (C++0x idk what they use) and typeinfo I was unable to see diff between e and f. Obviously it is probably fail on my part, so I would like to know if this is final C++11 standard behaviour.

Community
  • 1
  • 1
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277

1 Answers1

2

Yes and this is the standard behavior. It is written in §7.1.6.2[dcl.type.simple]/4, where:

The type denoted by decltype(e) is defined as follows:

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

Since c does not have parenthesis and is an id-expression (here, an identifier, see §5.1.1[expr.prim.general]/7), decltype(c) will be the type of c, which is int.

Since (c) do have parenthesis and is an lvalue (e.g. (c)=1 is a valid expression), decltype((c)) will be the lvalue reference type of type of (c), which is int&.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • good to know, btw any other part of C++ for which (SOMETHING) has one semantic and ((SOMETHING)) diff semantic meaning. – NoSenseEtAl Oct 02 '12 at 12:15
  • @NoSenseEtAl: `1-2*3` vs `(1-2)*3`, `int*x[10]` vs `int(*x)[10]`, ... ? – kennytm Oct 02 '12 at 12:23
  • you didnt understand me.... (1-2)*3 is the same as ((1-2))*3 – NoSenseEtAl Oct 02 '12 at 12:24
  • 2
    @NoSenseEtAl: Macro's. `FOO(x,y)` passes two parameters x and y, `FOO((X,Y))` passes one parameter `(X,Y)`. – MSalters Oct 02 '12 at 13:27
  • 1
    @NoSenseEtAl Off-topic but anyway, when `x` is an integer, `int f(int(x));` declares a function `f(int)`, and `int f((int(x)));` declares an integer `f=x` ([most vexing parse](http://stackoverflow.com/q/1424510/509868)) – anatolyg Oct 03 '12 at 19:57