decltype

auto 和decltye都可以用来定义变量类型,不同点:

1. auto 忽略顶层const, decltype不会;

2. auto推导的类型为绑定对象的类型,忽略* ,& ;而decltype会保留;

3, auto 在对数组推导时,将推导类型转化为指针; 而decltype对数组推导得到的仍然是数组。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
/*
decltype (e) :括号必不可少。在编译期推导表达式e的类型,但不对表达式进行计算。
Rule - 1. 如果e是一个标识符表达式或者类成员访问表达式,那么decltype(e)就是e所命名的实体的类型。如果没有此实体或者e命名了一个重载函数集,那么程序是无法编译通过的。
Rule - 2. 如果e是一个函数调用或者一个重载操作符调用(忽略e外面的括号),那么decltype(e)就是该函数的返回类型。
Rule - 3. 否则,假设e的类型是T:如果e是一个左值,则decltype(e)就是T&;否则(e是一个右值),decltype(e)就是T。/*
*/
class A{
public:
	int i;
};
//decltype(A) 
int foo(int i){
	return i;
};
decltype(foo) k;

int foo_1(int a){ return a; };
double foo_1(double a){ return a; };


template<typename T>
auto foo_2(T t) -> decltype(t) { return t; };

int odd[] = { 1, 3, 7 };
int even[] = { 2, 5, 8 };
decltype(odd) *foo_3(int i)
{
	return (i % 2) ? &odd : &even;
}
//
//decltype(foo_1) k;
decltype(foo_1(2.4)) test_foo_1; //double
decltype(foo_2(2))  test_foo_2;//int
decltype(foo_3(1)) test_foo_31;//int[3]*; 指向数组的指针


int main()
{
	int a = 0;
	decltype(a) test_a; // int
	decltype((a)) test_aa=a;//int&
	decltype(a = 0) test_a0=a;//int&

	const int b = 0;
	decltype(b) test_b = a;//const int; const属性被保留

	int &c = a;
	decltype(c) test_c = a;//int&; 引用类型的别名依然是引用

	int *p = &a;
	decltype(p) test_p = &a;//int* 指针类型的别名依然是指针
	decltype(&p) test_p1 =&p;//int**
	decltype(*p) test_p2 = a;//int&

	decltype(a) &test_a1 = a;//int&
	decltype(c) &test_c1 = a;//int&;&冗余,被忽略
	decltype(a) *test_p3 = &a;//int*
	decltype(p) *test_p4 = &p;//int**
	decltype(*&a) test_a3=a;//int&
	decltype(&*p) test_p5 = &a;//int*


	int A[5] = { 1, 2, 3, 4, 5 };
	decltype(A) test_A; //int[5] 数组类型保持原有类型,不做指针转化

	decltype(0) test_0;//int

	const char B[4] = "hel";
	decltype("C++") test_Cs=B;//const char[4]&


	string str("C++");
	decltype(str) test_str;//string

	vector<int> vec(5);
	decltype(vec) test_vec;//vector

	int i = 0;
	decltype(i++) test_i1; //int i++返回i的右值
	decltype(++i) test_i2 = a; // int& ++i返回i的左值

	auto t1 = ++i;//int
	auto t2 = ++i;//int
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值