cpp11特性(稳定性和兼容性)

cpp 11新特性(稳定性和兼容性)

1,原始字面量

1.1,简介

cpp 11提供了原始字面量R来表达不需要转义的的字符串

1.2,注意事项

  • 原始字面量的括号前后可添加一致的字符串如下str3会忽略所加的字符串

1.3,代码

using namespace std;
int main(){
     string str = "D:\hello\world\test.text";
    cout << str << endl;
    string str1 = "D:\\hello\\world\\test.text";
    cout << str1 << endl;
    string str2 = R"(D:\hello\world\test.text)";
    cout << str2 << endl;
    string str3 = R"hello(D:\hello\world\test.text)hello";
    cout << str3 << endl;
    return 0;
}

2,超长整形 longlong

2.1,简介

cpp 11新增了 longlong 类型

2.2,注意事项

  • longlong 的最大值根据平不同而不同最低有64位
  • 整形之间传参或者运算会发生隐式类型转换被称之为整形的提升

2.3,代码

#include <iostream>
using namespace std;

int main()
{
    long long max = LLONG_MAX;
    long long min = LLONG_MIN;
    unsigned long long ullMax = ULLONG_MAX;

    cout << "Max Long Long value: " << max << endl
        << "Min Long Long value: " << min << endl
        << "Max unsigned Long Long value: " << ullMax << endl;
    return 0;
}

3,类成员的快速初始化

3.1,简介

cpp 11引入了函数的非静态成员的就地初始化

3.2,注意事项

  • 静态成员变量必须在类外赋值
  • 非静态成员(常量,变量)和静态成员常量可以就地赋值
  • 可以使用 {} 和 = 两种赋值方式
  • 构造函数赋值会覆盖原来就地赋的值

3.3,代码

class test1{
private:
    static const int a{5};
};
class test3{
public:
 static int a;
};


class test2{
private:
    const int a{5};
    int b{5};
};


class test4{
public:
    test4():a(5){}
    int a{5};
};
int main()
{   
    test3::a = 5;
    return 0;
}

4,final和override

4.1,简介

cpp 11中使用 final 防止类被继承或虚函数被重写

使用 override 保证虚函数被重写

4.2,注意事项

  • final修饰函数时只能是虚函数

4.3,代码

//final
//重写虚函数
class Base
{
public:
    virtual void test()
    {
        cout << "Base class...";
    }
};

class Child : public Base
{
public:
    void test() final
    {
        cout << "Child class...";
    }
};

class GrandChild : public Child
{
public:
    // 语法错误, 不允许重写
    void test()
    {
        cout << "GrandChild class...";
    }
};
//防止继承
class Base
{
public:
    virtual void test()
    {
        cout << "Base class...";
    }
};

class Child final: public Base
{
public:
    void test()
    {
        cout << "Child class...";
    }
};

// error, 语法错误
class GrandChild : public Child
{
public:
};

//override
class Base
{
public:
    virtual void test()
    {
        cout << "Base class...";
    }
};

class Child : public Base
{
public:
    void test() override
    {
        cout << "Child class...";
    }
};

class GrandChild : public Child
{
public:
    void test() override
    {
        cout << "Child class...";
    }
};

5,模板的优化

5.1,简介

<<和>>更多的被解释为函数模板而不是操作符

函数模板默认参数的支持

5.2,注意事项

  • 函数模板参数全为默认时可省略 <> 构造而类模板不可
  • 类模板默认模板参数后都应该为默认模板参数
  • 推导出的模板类型优先级高于默认的

5.3,代码

#include <iostream>
using namespace std;

template <typename T=int>	// C++98/03不支持这种写法, C++11中支持这种写法
void func(T t)
{
    cout << "current value: " << t << endl;
}

int main()
{
    func(100);
    return 0;
}

6,数值类型和字符串的转化

6.1,简介

cpp 11提供了数值类型和字符串类型的类型转换

数值类型转换成字符串类型使用to_string()函数

字符串类型转换成数值类型使用sto系列函数(stoi,stof,stod等等)

6.2,注意事项

  • “123str”会被转换成123
  • “str123”则会报错

6.3,代码

#include<iostream>
#include<string>
using namespace std;

int main(){
    int a = 1;
    float b = 1.0;
    double c = 1.0;

    string sA = to_string(a);
    string sB = to_string(b);
    string sC = to_string(c);


    cout<<sA<<endl;
    cout<<sB<<endl;
    cout<<sC<<endl;
    
    sA = "123str";
    sB = "str123";

    //error
    int temp = stoi(sB);
    a = stoi(sA);

    cout<<a<<endl;

    return 0;
}

7,静态断言

7.1,简介

断言机制使得程序在在运行时违背某些前提条件时程序直接退出

cpp 11引入了静态断言在编译时即可检查条件并中断

7.2,注意事项

  • 静态断言的表达式必须得是常量表达式

7.3,代码

#include<iostream>
#include<string>
using namespace std;

int main(){
    const int i = 1;
    static_assert(i > 1,"error!");
    return 0;
}

8,noexcept

8.1,简介

用来修饰函数不会抛出异常,比单纯的throw()空参数列表效率更高

8.2,注意事项

  • 可以加上常量表达式
  • 常量表达式值为true时表示不会抛出异常
  • 为flase时则相反

8.3,代码

#include<iostream>
#include<string>
using namespace std;
//调用terminate终止程序
int func(int a) noexcept(true){
    if(a == 1){
        throw "error";
    }
    return a;
}
int main(){
    try{
        func(1);   
    }
    catch(string e){
        cout<<"string error";
    }
    return 0;
}