//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;
}
auto和decltype
最新推荐文章于 2017-06-10 16:11:45 发布