109 C++ 理解auto 类型推断,auto 应用场合

一。auto 类型推断

auto 用于变量的自动推断,原理是--在声明变量的时候,根据变量的初始值的类型,自动为此变量选择匹配的类型,而不需要我们显示指定类型


特点:1.auto 的类型推断是发生在编译期间的,所以不会影响程序执行期间的性能。
特点:2.auto 在编译期间就知道了类型,因此 auto 后面的变量需要立刻初始化。这样编译器才能推断它的实际类型。
            //那么编译的时候才能确定 auto 的类型和整个变量的类型。 
            //因此auto 是有类型的,(可以对照理解 参数模版 T ,参数模版T也是有类型的。)
            //然后 在编译期间就可以用真正的类型替换掉auto这个类型占位符了。
特点:3.auto的使用灵活,和指针,引用,const 等 限定符 结合使用。
特点:4.auto 的类型推断 和 函数模版的推断是 非常相似的。因此我们在理解的时候可以结合着函数模版中的 参数T,来对照理解 auto。也可以说 auto 这个东西也是类型声明的一部分。

auto & a;

    //总结一:传值方式的auto ,会抛弃引用,抛弃const 等限定符
    //总结二:引用传递的auto,引用会被抛弃,const 会被保留。

	using boost::typeindex::type_id_with_cvr;
	//之前我们在学习函数模版的时候,将函数模版中的参数分为3类。
	//a 指针或者引用类型但是不是万能引用
	//b 万能引用
	//c 传值方式(非指针,非引用)
	//在auto 的学习过程中,会参考这3类学习
	//c传值方式
	auto x = 27;// x= int,   auto =int
	const auto x2 = x;//x2 = const int,   auto = int

	// a 指针或者引用类型 但不是 万能引用。 auto 后面直接加 &,就是引用类型。
	const auto &xy = x2;//xy = const int & ,  auto = int

	cout << "xy = " << type_id_with_cvr<decltype(xy)>().pretty_name() << endl; //结果是int
	auto xy2 = xy; //xy2= int, auto = int;//注意这里,auto xy2 = xy 属于传值方式,引用类型会被抛弃,const 属性会被抛弃,把对方看成一个新副本。

	
	cout << "xy2 = " << type_id_with_cvr<decltype(xy2)>().pretty_name() << endl; //结果是int

	//总结一:传值方式的auto ,会抛弃引用,抛弃const 等限定符
	//总结二:引用传递的auto,引用会被抛弃,const 会被保留。
	auto &xy3 = xy;//引用传递 先前 xy = const int &,引用传递会抛弃 引用,因此xy3 = const int &, auto = const int

	auto y = new auto(100); //auto = int *, y = int *

	const auto *xp = &x;//先前 x= int, auto = int,xp = const int *
	auto *xp2 = &x;//先前 x= int, auto = int,xp2 =  int *
	auto xp3 = &x;//先前 x= int, auto = int *,xp3 =  int *   xp3没有声明为指针,但auto也把它推导成了指针类型

    //总结三, 万能引用
    auto && wnyy1 = x;//x是左值,auto = int&, wnyy1 = int&
    auto && wnyy2 = x2;//先前 x2 = const int,x2是左值,auto = int &,wnyy2 = const int &
    auto && wnyy3 = 100;//auto = int, wnyy3 = int &&

二。auto 类型对于数组和函数的推断

如果是值传递,数组会退化成指针。

如果是值传递,但是用引用接,数组就变成了 数组的引用 const char (&) [14];

const char mystr[] = "I Love china!" //mystr = const char[14];

auto myarr = mystr;//  auto = const char *    ,,   myarr = const char *

auto &myarr2 = mystr;    // myarr2 = const char (&) [14];

如果是函数

auto tempfunc = myfun3;// void(*)(double , int)//函数指针

auto &tempfunc2 = myfun3;// void(&)(double , int)//函数引用

三,auto 类型std::initializer_list的特殊推断

我们先来看,一个普通整型的定义和赋值

int x =10;

int x1(20);

int x2{30};

int x3 = {40};

如果换成auto 呢?

auto x =10;

auto x1(20);

auto x2{30};

auto x3 = {40};

然后打印x,x1,x2,x3的类型

会发现, x,x1,x2都是int

只有x3的类型是 std::initializer_list

那么这个initializer_list 是啥呢?initializer_list是C++11引入的新类型(类模版),表示某种特定的值的数组。

因此 auto x5 的写法会有build error

模版参数中是 initializer_list 以及调用方式

template <class T>
void fautofun(std::initializer_list<T> param) {

}

void main() {
	auto x = 10;
	auto x1(20);
	auto x2{ 30 };
	auto x3 = { 40 };

	using boost::typeindex::type_id_with_cvr;
	cout << "x = " << type_id_with_cvr<decltype(x)>().pretty_name() << endl; //结果是int
	cout << "x1 = " << type_id_with_cvr<decltype(x1)>().pretty_name() << endl; //结果是int
	cout << "x2 = " << type_id_with_cvr<decltype(x2)>().pretty_name() << endl; //结果是int
	cout << "x3 = " << type_id_with_cvr<decltype(x3)>().pretty_name() << endl; //结果是int

	//  x = int
	//	x1 = int
	//	x2 = int
	//	x3 = class std::initializer_list<int>

	auto x4 = { 50,60 };
	cout << "x4 = " << type_id_with_cvr<decltype(x4)>().pretty_name() << endl; //结果是int
	//x4 = class std::initializer_list<int>

	//auto x5 = { 50,60,89.8 };//build error,提示:无法推导“auto”类型

	//这样调用
	fautofun({ 12 });
}

四,   auto 不使用的场合举例

1.auto不能用于函数参数,比如void myfun(auto x, int y);

2.类内中的 普通成员变量不可以是 auto 类型

3.类内的静态成员变量 可以是auto类型,但是要在类内初始化  static const auto m_si = 15;//被允许

五。auto 适用场合举例

简化代码。

void main() {
	map<string, int> mymap = { {"wang",6},{"lisi",90} };
	mymap.insert(make_pair("nihao",23));

	map<string, int>::iterator iter;
	for (iter = mymap.begin(); iter != mymap.end(); ++iter)
	{
		cout << (*iter).first << "  =  " << (*iter).second << endl;
	}
	cout << " fengexian" << endl;
	for (auto iter = mymap.begin(); iter != mymap.end(); ++iter) {
		cout << (*iter).first << "  =  " << (*iter).second << endl;
	}
}

不确定类型时的应用。

class Teacher20 {
public:
	static int statictestfunc() {
		cout << "Teacher20 statictestfunc" << endl;
		return 0;
	}
};

class Teacher21 {
public:
	static double statictestfunc() {
		cout << "Teacher21 statictestfunc" << endl;
		return 100.8f;
	}
};
template <class T>
auto func61() {
	auto res =  T::statictestfunc();
	return res;
}

void main() {
	func61<Teacher20>();
	func61<Teacher21>();
}

  • 20
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值