Originally posted by: sumi_cj
The usage and advantage of the decltype specifier are introduced in Part I. For details, see
https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/c_11_the_decltype_specifier_part_i?lang=en
The deduction rules of decltype are introduced in Part II. For details, see
https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/c_11_the_decltype_specifier_part_ii?lang=en
In this section, I will state the inheritance of cv-qualifiers and the disposal of redundant symbols in decltype deductions.
cv-qualifier inheritance in decltype deductions
When you use decltype(expression) to get a type, and expression is an unparenthesized member variable of an object expression, the const or volatile qualifier of the parent object expression do not contribute to the result of decltype(expression). See the following example:
struct str{
int mem;
} x;
int main(){
const str y = {1};
volatile str * pz = &x;
decltype(y.mem) i; // int
decltype(pz->mem) j; // int
}
If expression declared in decltype(expression) is a parenthesized nonstatic non-reference class member variable, the const or volatile type qualifier of the parent object expression contributes to the result of decltype(expression). See the following example:
struct str{
int mem;
} x;
int main(){
const str y = {1};
volatile str * pz = &x;
int k;
decltype((y.mem)) i = k; // const int&
decltype((pz->mem)) j = k; // volatile int&
}
Redundant cv-qualifiers and operators in decltype deductions
The following redundant qualifiers or operators are ignored in decltype deductions:
-
The const qualfier
-
The volatile qualfier
-
The & operator
See the following example:
int main(){
int i = 1;
int& j = i;
const int k = 1;
volatile int m = 1;
decltype(i)& var1 = i; //int&
decltype(j)& var2 = i; // int&, the redundant & operator is ignored
const decltype(k) var3 = 1; // const int, the redundant const qualifier is ignored
volatile decltype(m) var4 = 1; // volatile int, the redundant volatile qualifier is ignored
}
Please be aware that redundant * operators will not be ignored in decltype deductions. See the following example:
int main(){
int i = 1;
int * pj = &i;
decltype(pj) * var1 = &i; // error
decltype(pj) * var2 = &pj; // ok. The type of var2 is int **p
}
#C/C++-compilers-for-AIX#C/C++andFortran