Home >Backend Development >C++ >What\'s the Impact of Parentheses in `decltype((...))`?
What Does Double Parentheses in decltype((...)) Mean?
The C standard defines the behavior of the decltype((...)) syntax in section 7.6.1.2/4 of the FCD. This syntax allows programmers to deduce the type of an expression, as demonstrated in the following example:
<code class="cpp">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&</code>
The presence of parentheses around the expression in decltype((a->x)) makes a significant difference in the deduced type. Without the parentheses, the type is simply double, indicating the return type of the member access (a->x).
However, with the parentheses, the expression becomes an lvalue. According to the standard, if e is an lvalue, decltype(e) is T&, where T is the type of e. In this case, T is double, and the deduced type is therefore const double&.
Therefore, the parentheses in decltype((a->x)) force the deduction to treat the expression as an lvalue, resulting in a different type than if the parentheses were omitted.
The above is the detailed content of What\'s the Impact of Parentheses in `decltype((...))`?. For more information, please follow other related articles on the PHP Chinese website!