Significance of parentheses in decltype((c))?

前端 未结 2 2004
花落未央
花落未央 2020-11-29 23:50

I was reading this article on Wikipedia regarding C++11 Type Inference feature.

There is an example and I quote:

#include 
int main() {         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 00:23

    I found a good description here. It describes the difference between:

    struct A { double x; };
    const A* a = new A();
    ...
    decltype(a->x) x4; // type is double
    decltype((a->x)) x5; // type is const double&
    

    and I quote:

    The reason for the difference between the latter two invocations of decltype is that the parenthesized expression (a->x) is neither an id-expression nor a member access expression, and therefore does not denote a named object. [13]

    Because the expression is an lvalue, its deduced type is "reference to the type of the expression", or const double&. [10]

提交回复
热议问题