auto和decltype

//c++将常量存储在符号表中,不为const分配存储空间
#include <iostream>
using namespace std;

struct
{
    char* name;
}anon_u;
struct
{
    int d;
    decltype(anon_u) id;
}anon_s[100];

int main()
{
    decltype(anon_s) as;
    int m = 1;
    cout << &m << endl;
    int& a = m;
    cout << &a << endl;
    auto c = a; //c为一个整数(忽略引用)

    int i = 0;
    const int j = i, &j1 = j;
    cout << &j << endl;
    auto p = j; //int
    auto q = j; //int

    auto r = &i;  //r为一般指针
    r = &m;
    *r =1;

    cout << r << endl;
    auto s = &j1;   //s为常量指针
    s = &m;
    //*s = 3; 编译不通过

    const int d = 0;
    decltype(d) e = 0; //const int
    decltype(d + 2) f = 0; //返回表达式结果的类型
    //如果decltype使用表达式的结果类型可以作为一条赋值语句的左值,那么decltype返回一个引用类型,例如解引用操作和变量加括号的类型
    decltype(*s) g = 3; //返回int&
    decltype((d)) h = 1;//返回int& (d)为一个表达式

    //如果表达式类型本身就是一个引用类型,那么decltype返回对应类型的引用类型
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值