音乐网站建设方案书模板,wordpress注册激活码,学校网站建设状况,个体工商户注册网站不允许使用auto的四个场景#xff1a;
1.不能作为函数参数使用#xff0c;因为只有在函数调用的时候才会给函数参数传递实参#xff0c;auto要求必须要给修饰的变量赋值#xff0c;因此二者矛盾。
代码如下:
//error
int func(auto a, auto b)
{cout a
1.不能作为函数参数使用因为只有在函数调用的时候才会给函数参数传递实参auto要求必须要给修饰的变量赋值因此二者矛盾。
代码如下:
//error
int func(auto a, auto b)
{cout a b endl;
}2.不能用于类的非静态成员变量的初始化。
代码如下:
class Test
{auto a 0;//errorstatic auto b 2;//error,类的静态非常量成员不允许在类内部直接初始化static const auto c 10;//ok
};3.不能使用auto关键字定义数组。
代码如下:
int func()
{int array[] { 1,2,3,4,5 };//okauto t1 array;//okt1 - int *auto t2[] array;//errorauto无法定义数组auto t3[] { 1,2,3,4,5 };//errorauto无法定义数组
}4.无法使用auto推导出模板参数。
代码如下:
template typename T
struct Test
{};int func()
{Testdouble t;Testauto t1 t;//error,无法推导模板类型return 0;
}