C++11学习笔记(二)

来源:互联网 发布:大数据就业前景 编辑:程序博客网 时间:2024/05/23 05:07

【右尖括号>的改进】

在C++98中,我们会遵循一个规则——实例化模板时,如果出现2个连续的右尖括号 > ,则需要用一个 空格 隔开,否则会被编译器误以为是 右移 符号


template <int i> class X{};template <class T> class Y{};Y<X<1> > x1;    // 编译成功Y<X<2>> x2;     // 编译失败

C++11中要求编译器智能的判断 >> 是否是右移符号。不过,如果在某种情况下,我们在实例化模板的同时进行右移操作

template <int i> class X {};X<1 >> 5> x ; 

可以加个括号来改变优先级

template <int i> class X {};X<(1 >> 5)> x ; 


【静态类型、动态类型与类型推导】

C++被称之为静态类型语言,而Python,JavaScript则被称之为动态类型语言。从技术上严格地讲,其区别在于 对变量进行类型检测的时期。


静态类型的类型检测发生在编译阶段,动态类型的类型检测发生在运行阶段。


动态类型语言的实现,得益于 类型推导技术。而类型推导同样可用在静态类型语言中,所以C++11重定义了auto关键字。

#include <iostream>using namespace std;int main() {    auto name = "world.\n";    cout << "hello, " << name;}

(在C++98中,auto用于标识局部变量类型,因而几乎没有被使用过)


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;}

从代码中对 z 的声明可以看出,auto不是一种类型声明,更像是一个“占位符”,编译器在编译时会将auto换成实际类型。


auto的优势之简化代码

对比两段代码

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

下面是使用auto的

#include <string>#include <vector>void loopover(std::vector<std::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 <iostream>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();     // intauto & h = bar();   // int&<strong></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 doubleconst auto & b = foo();     // b: const double&volatile auto * c = bar();  // c: volatile float*auto d = a;                 // d: doubleauto & 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};     // 使用初始化列表的autoauto z = new auto(1);   // 可以用于new


auto的使用限制

以下四种情况都是C++11标准所不允许的,虽然它们看起来可行
#include <vector>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};}

与auto类似, decltype也能用于类型推导,不过使用方式不太一样。因为当前并未搞懂decltype,这里就不误导人了。。












1 0
原创粉丝点击