noexcept修饰符
void func3() throw(int,char)//只能够抛出int和char类型的异常
{
throw 0;
}
void blockThrow()throw()//代表函数不能抛出异常,如果抛出,就会异常
{
throw 1;
}
//C++11使用noexcept代替throw()
void blockthrow() noexcept
{
throw 2;
}
nullptr
在C++98中,字面常量0既可以是一个整形数字,也可以是一个无类型的指针常量,但是编译器默认情况下将其看成是一个整形常量,如果要按照指针方式来使用,必须对其进行强转(void*)0。nullptr是为了解决原来C++中NULL的二义性问题而引进的一种新的关键字。
1) 在使用nullptr表示指针空值时,不需要包含头文件,因为nullptr是C++11作为关键字引入的
2) 在C++11中,sizeof(nullptr)与sizeof((void*) 0)所占字节数相同
3) 为了提高代码的健壮性,在后续表示指针空值的代码中建议最好使用nullptr
void fun(int a)
{
cout << __LINE__ << " a = " << a << endl;
}
void fun(int *p)
{
cout << __LINE__ << " p = " << p << endl;
}
void test()
{
int *p1 = nullptr;
int *p2 = NULL;
if (p1 == p2) //成立
{
cout << "equal" << endl;
}
int a = NULL;//成功
int b = nullptr;//类型不匹配
fun(0);//调用 fun(int a)
fun(NULL);//调用 fun(int a)
fun(nullptr);//调用 fun(int *p)
}
原生字符串字面值
原生字符串字面值(raw string iteral)使用用户书写的字符串(所见即所得)。C++11中原生字符串的声明相当简单,只需在字符串前加入前缀,即字符R,并在引号中使用括号左右标识,就可以声明该字符串字面量为原生字符串了。
void test2()
{
cout << R"(hello,\n world)" << endl;
string str = R"(hello\4 \r
abc,mike
hello)";
cout << endl;
cout << str << endl;
}