auto的基本用法

int main()
{
double foo();
auto x = 1; // x的类型为int
auto y = foo(); // y的类型为double
struct m { int i; }str;
auto str1 = str; // str1的类型是struct m
auto z; // 无法推导,无法通过编译
z = x;
}
auto的优势之简化代码,对比两段代码
#include
#include

void loopover(std::vectorstd::string & vs)
{
std::vectorstd::string::iterator i = vs.begin();
for (; i < vs.end(); i++)
{
// 一些代码
}
}

下面是使用auto的
#include
#include

void loopover(std::vectorstd::string & vs)
{

for (auto i = vs.begin(); i < vs.end(); i++) 

{
// 一些代码
}
}

很明显,通过auto,使用STL变得更加容易。
auto的优势之免除声明时的麻烦

class PI
{
public:
double operator* (float v)
{
return (double)val * v; // 这里精度被扩展了
}
const float val = 3.1415927f;
};

int main()
{
float radius = 1.7e10;
PI pi;
auto circumference = 2 * (pi * radius);
}

如果使用 float 代替 auto 标识circumference的话,就不能体现出PI类的好处了。
当然,auto并不是万能的,比如下面这个例子
#include
using namespace std;

int main()
{
unsigned int a = 4294967295; //最大的unsigned int值
unsigned int b = 1;
auto c = a + b; // c的类型依然是unsigned int

cout << "a = " << a << endl;    // a = 4294967295
cout << "b = " << b << endl;    // b = 1
cout << "a + b = " << c << endl;// a + b = 0
return 0;

}

auto的优势之支持泛型编程
template<typename T1, typename T2>
double Sum(T1 & t1, T2 & t2)
{
auto s = t1 + t2; // s的类型会在模板实例化时被推导出来
return s;
}

int main()
{
int a = 3;
long b = 5;
float c = 1.0f, d = 2.3f;
auto e = Sum(a, b); // s的类型被推导为long
auto f = Sum(c, d); // s的类型被推导为float
}
不仅是在模板中,在宏定义中,也可以使用auto来提升效率
#define Max1(a, b) ((a) > (b)) ? (a) : (b)
#define Max2(a, b) ({
auto _a = (a);
auto _b = (b);
(_a > _b) ? _a: _b; })

int main()
{
int m1 = Max1(1 * 2 * 3 * 4, 5 + 6 + 7 + 8);
int m2 = Max2(1 * 2 * 3 * 4, 5 + 6 + 7 + 8);
}
Max2明显比Max1效率要高

auto指示符与指针、引用混合使用
int x;
int * y = &x;
double foo();
int & bar();

auto * a = &x; // int*
auto & b = x; // int&
auto c = y; // int*
auto * d = y; // int*
auto * e = &foo(); // 编译失败, 指针不能指向一个临时变量
auto & f = foo(); // 编译失败, nonconst的左值引用不能和一个临时变量绑定
auto g = bar(); // int
auto & h = bar(); // int&
< / strong>

对于 a、c、d而言,声明为 auto 或 auto* 都一样。但要声明一个变量 作为另一个变量的引用,则必须使用auto&,如同本例中的b、h那样。

auto指示符与cv限制符
auto与volatile和const也存在着一些联系,C++11允许auto和cv限制符一起使用
double foo();
float * bar();

const auto a = foo(); // a: const double
const auto & b = foo(); // b: const double&
volatile auto * c = bar(); // c: volatile float*

auto d = a; // d: double
auto & e = a; // e: const double &
auto f = c; // f: float *
volatile auto & g = c; // g: volatile float * &

可以看出,通过auto并不能带走变量的 常量性 或 易失性。
auto 也可以在同一赋值语句中声明多个变量,但这些变量的类型必须相同
auto x = 1, y = 2; // x和y的类型均为int

// m是一个指向const int类型变量的指针, n是一个int类型的变量
const auto* m = &x, n = 1;
auto i = 1, j = 3.14f; // 编译失败
auto o = 1, &p = o, *q = &p; // 从左向右的推导
对auto声明的变量同样可以使用C++11中新的初始化列表

#include <initializer_list>

auto x = 1;
auto x1(1);
auto y{ 1 }; // 使用初始化列表的auto
auto z = new auto(1); // 可以用于new

auto的使用限制

以下四种情况都是C++11标准所不允许的,虽然它们看起来可行
#include
using namespace std;

void fun(auto x = 1) {} // 1: auto函数参数,无法通过编译
struct str
{
auto var = 10; // 2: auto非静态成员变量,无法通过编译
};

int main()
{
char x[3];
auto y = x;
auto z[3] = x; // 3: auto数组,无法通过编译

// 4: auto模板参数(实例化时),无法通过编译
vector<auto> x = { 1 };

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值