C++Primer第五版:练习2.33 2.34 2.35 2.36 2.37 2.38

练习2.33 2.34
d非法:d是一个整型指针
e非法:e是一个指向整数常量的指针
g非法:g是一个整型常量引用

练习2.35
i:整型常量
j:整型
k:整型常量引用
p:整型指针
j2:整型常量
k2:整型常量引用

练习2.36

#include<iostream>

int main()
{
    
    
	int a = 3, b = 4;
	decltype(a) c = a;
	decltype((b)) d = a;
	++c;
	++d;
}

a:int,4
b:int,4
c:int,4
d:引用,4

练习2.37

#include<iostream>

int main()
{
    
    
	int a = 3, b = 4;
	decltype(a) c = a;
	decltype(a = b) d = a;
}

a:int,3
b:int,4
c:int,3
d:引用 3

练习2.38

#include<iostream>

int main()
{
    
    
	const int i = 5;
	auto a = i;//a为int,忽略顶层const
	const auto a2 = i;//a2为const int
	decltype(i) b = 5;//b为对常量的引用
}

decltype处理顶层const和引用的方式与auto不同,返回变量或表达式的类型包括顶层const和引用
decltype的结果类型与表达式形式有关,加上括号得到的类型是引用

猜你喜欢

转载自blog.csdn.net/Xgggcalled/article/details/108940891
今日推荐