C++11特性-易用性改进

1.字符串的原始字面量

        表达式:R"xxx(原始字符串)xxx"或者R"(原始字符串)",xxx要写的话,必须一样

	//两种申明形式
    string a = "aaa";//没有转义的时候就是原始字面量
	string b = R"aaa(bbb)aaa";//申明为R"xxx()xxx"
	string c = R"(ccc)";//申明为R"()"
	string d = R"aaaa(ddd)aaaa";//申明为R"xxx()xxx"
	cout << a<<b<<c<<d << endl;

        意义:避免字符串中有转义字符时出现转义的问题,解决字符串过长(字符串写在多行中)而不想使用连接字符(\)的问题

    //避免转义
	string str = "hello\word\ttt";
	string strR = R"(hello\word\ttt)";
	cout << str << endl<< strR << endl;
	string  str2 = "hello\\word\\ttt";
	cout << str2 << endl;
    //解决字符串过长(字符串写在多行中)而不想使用连接字符(\)的问题
	string str = "dsfhk\
		dsfklhj\
		dsklfhjsd\
		<sd>\
		sdfkgjyhk\
		dsfg\
		dfsgf\
		dsrfgdrf";
	string strR = R"(dsfhk
		dsfklhj
		dsklfhjsd
		<sd>
		sdfkgjyhk
		dsfg
		dfsgf
		dsrfgdrf)";

		cout << str << endl << strR << endl;

2.用户指定的字面量(自定义后缀)

        operator"" xxx

3.指针空值类型-nullptr

        在C程序中,NULL表示(void*)0;在C++程序中,NULL表示0

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

void func(char* a) {
	cout << "func(char* a)" << endl;
}

//调用
func(10);
func(NULL);
func(nullptr);

          C++中void*不能隐式转换成其他类型的指针

    void* a = NULL;//NULL为0或者void *
	int* b = NULL;//NULL为0或者void *
	double* c = NULL;//NULL为0或者void *
	char* d = NULL;//NULL为0或者void *

         nullptr不能匹配类型;可以隐式的匹配指针类型,不能匹配类型。

	void* ptr = nullptr;//nullptr隐式转成void*
	int* test2 = nullptr;//nullptr隐式转成int*
	double* test3 = nullptr;//nullptr隐式转成double*
	char* test4 = nullptr;//nullptr隐式转成char*

4.常量-constexpr

 c11之前的const有两个意思,修饰常量与变量只读

        constexpr修饰常量表达式

        常量变达式:多个常量(值不变)组成,且在编译过程中得到计算结果的表达式,达到提高程序执行效率的结果

        定义常量const与constexpr是等价的,们都在编译过程得到结果

    //定义常量时,const与constexpr是等价的
	const int c = 33;//常量达表式
	const int d = c + 1;//常量达表式

	constexpr int h = 45;//常量达表式
	constexpr int k = h + 3453;//常量达表式

        对于C++内置数据类型可以使用constexpr修饰,使用class与struct自定义的数据类型不能使用constexpr修饰

        常量表达式函数:constexpr修饰函数返回值的函数(普通函数、类成员\构造函数、模板函数),发生在编译阶段

        常量表达式函数要求:

                1.函数必须有返回值且返回值必须是常量表达式

                2.函数使用前必须有对应的定义语句

                3.整个函数体,不能出现常量表达式之外的语句(return除外)不能出现如using、typedef、static_assert断言、循环等

                4.使用前必须先定义

                5.几乎只有一个return语句(极少数除外)

//常量表达式函数
constexpr int funConstexpr3() {
	constexpr int a = 9;//a为常量
	return a;
}

//constexpr修饰模板函数,会根据传入参数类型确定是否为常量表达式函数
template <typename TT>
constexpr TT display(TT t) {
	return t;
}

//constexpr修饰构造函数,函数体必须为空.初始化必须为成员初始化列表
class MyClass3
{
public:
	constexpr MyClass3():h(67) {}

	int h;
};

        在C++中建议把const与constexpr区分(即表示“只读”使用const,"常量"使用constexpr)

        常量表达式的为类的构造函数时,函数体必须为空;必须使用常量表达式来给初始化列表赋值

5.静态断言static_assert

        C语言的assert在运行阶段,对断言进行检查。条件为真,执行程序;条件为假,执行about()

        C++的static_assert在编辑阶段对断言进行检查。

6.noexcept修饰符

        代表函数不能抛出异常,抛出异常就报错(使用noexcept答题throw())

//函数不能抛出异常,抛出异常则报错
void test_noexcept1() throw() {
	//throw 2;
}

//函数不能抛出异常,抛出异常则报错
//C++11用noexcept代替throw()
void test_noexcept() noexcept {
	//throw 1;
}

7.强类型枚举

        在enum后面加struct或者class修饰,使用时必须加上作用域;并且可以指定成员变量的类型

	//一般枚举
	enum MyEnum{OK,Error};
	//“test_enum::OK” : 重定义;以前的定义是“枚举数”
	//enum MyEnum1{OK,Error};

	//强类型枚举
	//在enum后面加struct或者class修饰

	enum class MyEnum3 { OK, Error };
	enum struct MyEnum4{OK,Error};

	//使用时必须指定哪个作用域
	//MyEnum3 flag = OK; //error
	MyEnum3 flag = MyEnum3::OK; //使用时必须加上作用域

	//可以指定成员变量类型
	enum class MyEnum3 :char{ OK, Error };
	enum struct MyEnum4 :int{ OK, Error };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值