天天看点

C++11:noexcept修饰符、nullptr、原生字符串字面值

noexcept修饰符

void func3() throw(int, char) //只能够抛出 int 和char类型的异常
{//C++11已经弃用这个声明
     throw 0;
}

void BlockThrow() throw() //代表此函数不能抛出异常,如果抛出,就会异常
{
    throw 1;
}

//代表此函数不能抛出异常,如果抛出,就会异常
//C++11 使用noexcept替代throw()
void BlockThrowPro() noexcept
{
    throw 2;
}      

nullptr

void func(int a)
{
    cout << __LINE__ << " a = " << a <<endl;
}

void func(int *p)
{
     cout << __LINE__ << " p = " << p <<endl;
}

int main()
{
    int *p1 = nullptr;
    int *p2 = NULL;

    if(p1 == p2)
    {
        cout << "equal\n";
    }

    //int a = nullptr; //err, 编译失败,nullptr不能转型为int

    func(0); //调用func(int), 就算写NULL,也是调用这个
    func(nullptr);

    return 0;
}      

原生字符串字面值

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

int main(void)
{
    cout << R"(hello, \n world)" << endl;
    string str = R"(helo \4 \r 
    abc, mike
    hello\n)";
    cout << endl;
    cout << str << endl;

    return 0;
}      

继续阅读