天天看點

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

繼續閱讀