C++11是C++的第二个主要版本,并且是从C++98起的最重要更新。C++11是C++编程语言的一个重要版本,于2011年正式发布。它引入了许多新特性和改进,极大地增强了 C++ 的功能和易用性。下面介绍它的一些主要特性:
在C++98中一般数组和结构体支持使用{}初始化。
struct point { int x; int y; }; int main() { int arr1[5] = {1,2,3,4,5}; point p = { 1,2 }; return 0; }
struct point { int x; int y; }; class Date { public: Date(int year = 1, int month = 1, int day = 1) :_year(year) , _month(month) , _day(day) { cout << "Date(int year int month , int day)" << endl; } Date(const Date& d) :_year(d._year) ,_month(d._month) ,_day(d._day) { cout << "Date(const Date& d)" << endl; } private: int _year; int _month; int _day; };
//C++11 //内置类型支持{}初始化 int x = { 2 }; //自定义类型 //本质是{2025,1,1}构造出临时对象,再拷贝给d1,但是编译器优化为直接用{2025,1,1}构造d1 Date d1 = { 2025,1,1 }; //这里的d2引用的是{2024,7,2}的临时对象 const Date& d2 = { 2024,7,2 }; //需要注意的是C++98支持单参数时类型转换,也可以不用加{} Date d3 = { 2025 }; Date d4 = 2025;
//可以省略掉= int x2{ 2 }; point p1{ 1,2 }; Date d6{ 2025,1,2 }; const Date& d7{ 2024,8,15 };
vector<Date> v; //有名对象传参 v.push_back(d6); //匿名对象传参 v.push_back(Date(2025, 1, 2)); //比起有名对象和匿名对象,{}初始化更有性价比 v.push_back({ 2025,1,2 });


vector<int> v1 = { 1,2,3,4,5 }; vector<int> v2 = { 1,2,3,4,5,6 }; //这里pair对象的{}初始化和map的initializer_list构造结合到一起了 map<string, string> dict = { {"sort","排序 "},{"string","字符串"} };
C++98中就有引用的语法,而C++11中新增了右值引用的语法特性,之前的引用叫做左值引用。无论左值引用还是右值引用,都可以理解为是在给变量取别名。
//左值,可以取地址 //以下均为左值 int* p = new int(0); int b = 1; const int c = b; *p = 10; string s("1111111"); s[0] = 'x'; double x = 1.1, y = 2.2; //右值,不能取地址 //以下几个均为右值 10; x + y; string("111111");
//左值,可以取地址 //以下均为左值 int* p = new int(0); int b = 1; const int c = b; *p = 10; string s("1111111"); s[0] = 'x'; double x = 1.1, y = 2.2; //右值,不能取地址 //以下几个均为右值 10; x + y; string("111111"); //左值引用,给左值取别名 int& r1 = b; int*& r2 = p; int& r3 = *p; string& r4 = s; char& r5 = s[0]; //左值引用不能直接引用右值,需加上cosnt const int& rx1 = 10; const double& rx2 = x + y; const string& rx3 = string("111111");
//左值,可以取地址 //以下均为左值 int* p = new int(0); int b = 1; const int c = b; *p = 10; string s("1111111"); s[0] = 'x'; double x = 1.1, y = 2.2; //右值,不能取地址 //以下几个均为右值 10; x + y; string("111111"); //右值引用 int&& rr1 = 10; double&& rr2 = x + y; string&& rr3 = string("111111"); //右值引用不能直接引用 左值,但可以引用 move(左值) int&& rrx1 = move(b); int*&& rrx2 = move(p); int&& rrx3 = move(*p); string&& rrx4 = (move)(s); string&& rrx5 = (string&&)s;

//右值引用 int&& rr1 = 10; double&& rr2 = x + y; string&& rr3 = string("111111"); //这里要注意的是,rr1的属性是左值,要想被右值引用绑定,除非move一下 int&& a = move(rr1);
右值引用可用于为临时对象延长生命周期,const的左值引用也能延长临时对象生存期,但这些对象无法被修改。
string s1 = "Test"; //s1+s1生成临时对象 const string& s2 = s1 + s1; //const左值引用延长生命周期 string&& s3 = s1 + s1; //右值引用延长生命周期 s3 += "Test"; cout << s3 <<endl;
void f(int& x) { cout << "f(int& x)" << endl; } void f(const int& x) { cout << "f(const int& x)" << endl; } void f(int&& x) { cout << "f(int&& x)" << endl; } int main() { int x = 1; const int y = x; f(x);//调用f(int& x) f(y);//调用f(const int& x) f(3);//调用f(int&& x) f(move(x));//调用f(int&& x) //右值引用变量是左值属性的 int&& z = 1; f(z); //调用f(int& x) f(move(z));//调用f(int&& x) return 0; }
移动语义是现代编程语言(如C++11及更高版本、Rust)中用于优化资源管理的重要机制。其核心目标是避免不必要的拷贝,通过转移资源所有权(而非复制)提升程序性能,尤其在处理动态内存、文件句柄等资源时效果显著。
为什么需要移动语义?
对于包含动态内存或系统资源的对象(如std::vector,std::string等),拷贝构造函数会深度复制所有数据,导致性能开销。
直接窃取临时对象(如右值)的资源,避免复制。
移动构造函数与移动赋值运算符
移动构造函数:接受右值引用参数,转移资源。
class MyClass {
public:
// 移动构造函数
MyClass(MyClass&& other) noexcept
: data_(other.data_)
,size_(other.size_)
{
other.data_ = nullptr; // 置空原对象,避免重复释放
other.size_ = 0;
}
private:
int* data_;
size_t size_;
};移动赋值运算符:类似移动构造,处理对象赋值。
MyClass& operator=(MyClass&& other) noexcept {
if (this != &other)
{
delete[] data_; // 释放当前资源
data_ = other.data_; // 转移资源
size_ = other.size_;
other.data_ = nullptr;
other.size_ = 0;
}
return *this;
}使用场景
1,显示触发移动语义
使用std:move将左值转化为右值:
std::vector<int> a = {1, 2, 3};
std::vector<int> b = std::move(a); // a变为空,资源转移给b注意:被移动后的对象处于有效但未定义状态(通常为空),不可再使用其值。
2,返回值优化
编译器自动优化函数返回的临时对象,避免拷贝:
std::vector<int> createVector() {
std::vector<int> v = {1, 2, 3};
return v; // 编译器可能直接构造v到调用方,无需移动或拷贝
}3,STL容器的移动支持
标准库容器(如std::vector、std::string)已实现移动语义:
std::vector<std::string> vec;
std::string s = "data";
vec.push_back(std::move(s)); // 移动s到容器,避免拷贝字符串内容常见问题与缺陷
1,误用std:move
对局部变量过早移动,导致后续访问未定义
std::vector<int> a = {1, 2, 3};
std::vector<int> b = std::move(a);
std::cout << a.size(); // 未定义行为,a可能为空2,未实现移动语义的类
std::move)。
3,异常安全
移动操作应标记为noexcept,否则某些容器(如std::vector)可能仍选择拷贝。
代码示例:自定义类的移动语义
#include <iostream>
#include <utility> // for std::move
class Buffer {
public:
Buffer(size_t size) : size_(size), data_(new int[size]) {}
// 移动构造函数
Buffer(Buffer&& other) noexcept
: size_(other.size_), data_(other.data_) {
other.size_ = 0;
other.data_ = nullptr;
}
// 移动赋值运算符
Buffer& operator=(Buffer&& other) noexcept {
if (this != &other) {
delete[] data_;
data_ = other.data_;
size_ = other.size_;
other.data_ = nullptr;
other.size_ = 0;
}
return *this;
}
~Buffer() { delete[] data_; }
private:
size_t size_;
int* data_;
};
int main() {
Buffer a(100);
Buffer b = std::move(a); // 调用移动构造函数
Buffer c(200);
c = std::move(b); // 调用移动赋值运算符
return 0;
}C++11以后,STL容器的push和insert接口增加了右值引用版本。
以vector容器的push_back为例:

当实参是一个左值时,继续调用拷贝构造进行拷贝;当实参是一个右值时,容器内部调用移动构造,提高效率。
int main() { typedef int& lref; typedef int&& rref; int n = 0; lref& r1 = n; //r1的类型是int& lref&& r2 = n; //r2的类型是int& rref& r3 = n; //r3的类型是int& rref&& r4 = 3; //r4的类型是int&& return 0; }
//根据引用折叠规则,f1实例化后总是一个左值引用 template <class T> void f1(T& x) {} //根据引用折叠规则,f2实例化后可以是一个左值引用,也可以是一个右值引用 template <class T> void f2(T&& x) {} int main() { //折叠->实例化为 f1(int& x) f1<int>(n); //f1(0); //报错 //折叠->实例化为 f1(int& x) f1<int&>(n); //f1<int&>(0); //报错 //折叠->实例化为 f1(int& x) f1<int&&>(n); //f1<int&&>(0); //报错 //折叠->实例化为 f1(const int& x) f1<const int&>(n); f1<const int&>(0); //折叠->实例化为 f1(const int& x) f1<const int&&>(n); f1<const int&&>(0); //没有折叠->实例化为 f2(int&& x) //f2<int>(n); //报错 f2<int>(0); //折叠->实例化为 f2(int& x) f2<int&>(n); //f2<int&>(0); //报错 //折叠->实例化为 f2(int&& x) //f2<int&&>(n); //报错 f2<int&&>(0); return 0; }

std::forward:用于在转发参数时保持其原始的类别 。void func(int& x) { cout << "左值引用" << endl; } void func(const int& x) { cout << "const 左值引用" << endl; } void func(int&& x) { cout << "右值引用" << endl; } void func(const int&& x) { cout << "const 右值引用" << endl; } template <class T> void Function(T&& t) { func(t); } int main() { Function(10); //10是右值 Function(int&& t) 右值 int a;、 Function(a); //a是左值 Function(int& t) 左值 Function(move(a)); // Function(int&& t) 右值 const int b = 8; Function(b); //Function(const int& t) const左值 Function(move(b)); //Function(const int&& t) const 右值 return 0; }
上面的代码是没有使用 完美转发的场景,运行结果如下:

可以看出,在将t传给下一层的func函数时,匹配的都是左值引用。
经过完美转发后的代码和运行结果:
template <class T> void Function(T&& t) { //func(t); func(forward<T>(t)); }
