原始字面量 R
定义方式为:R “xxx(原始字符串)xxx”
#include<iostream>
#include<string>
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;
return 0;
}
在R “xxx(raw string)xxx” 中,原始字符串必须用括号()括起来,括号的前后可以加其他字符串,所加的字符串会被忽略,并且加的字符串必须在括号两边同时出现。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = R"(D:\hello\world\test.text)";
cout << str1 << endl;
string str2 = R"luffy(D:\hello\world\test.text)luffy";
cout << str2 << endl;
#if 0
string str3 = R"luffy(D:\hello\world\test.text)robin"; // 语法错误,编译不通过
cout << str3 << endl;
#endif
return 0;
}
输出结果:
D:\hello\world\test.text
D:\hello\world\test.text
右值引用
C++11 增加了一个新的类型,称为右值引用( R-value reference),标记为 &&。在介绍右值引用类型之前先要了解什么是左值和右值:
lvalue 是 loactor value 的缩写,rvalue 是 read value 的缩写
左值是指存储在内存中、有明确存储地址(可取地址)的数据;
右值是指可以提供数据值的数据(不可取地址);
左值和右值是独立于他们的类型的,右值引用类型可能是左值也可能是右值。
编译器会将已命名的右值引用视为左值,将未命名的右值引用视为右值。
auto&&或者函数参数类型自动推导的T&&是一个未定的引用类型,它可能是左值引用也可能是右值引用类型,这取决于初始化的值类型(上面有例子)。
通过右值推导 T&& 或者 auto&& 得到的是一个右值引用类型,其余都是左值引用类型。