C++11学习笔记(二)

【右尖括号>的改进】

在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();     // int
auto & 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 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 <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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值