[C++11阅读][3-3-4]decltype类型推导(下)

cv限制符

decltype可以带走cv限制符,这一点跟auto不同,auto带不走。
但cv限制符不会扩散到成员变量,也比较好理解。

#include <type_traits>
#include <iostream>
using namespace std;
const int ic = 0;
volatile int iv;
struct S { int i; };
const S a = {0};
volatile S b;
volatile S* p = &b;
int main() {
    cout << is_const<decltype(ic)>::value << endl;  // 1
    cout << is_volatile<decltype(iv)>::value << endl;  // 1
    cout << is_const<decltype(a)>::value << endl;  // 1
    cout << is_volatile<decltype(b)>::value << endl;  // 1
    cout << is_const<decltype(a.i)>::value << endl;  // 不会扩散到成员变量,0
    cout << is_volatile<decltype(p->i)>::value << endl;  // 不会扩散到成员变量,0
}

冗余符号

与auto一样,decltype后面可以跟着&和,多余的&不会变成右值引用,而是被吃掉,但多余的会累加上,变成指针的指针**,与auto不一样。

#include <type_traits>
#include <iostream>
using namespace std;
int i = 1;
int & j = i;
int * p = &i;
const int k = 1;
int main() {
    decltype(i) & var1 = i;  // int&
    decltype(j) & var2 = i;  // 也是int&,多余的一个&被吃掉
    cout << is_lvalue_reference<decltype(var1)>::value << endl;  // 1
    cout << is_rvalue_reference<decltype(var2)>::value << endl;  // 0
    cout << is_lvalue_reference<decltype(var2)>::value << endl;  // 1
    // decltype(p)* var3 = &i;  // compile err
    decltype(p)* var3 = &p;  // var3是int**
    auto* v3 = p;  // v3是int*,auto跟decltype不一样,会吃掉多余的*
    v3 = &i;
    const decltype(k) var4 = 1;  // 多余的const被吃掉,理所应当
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。