c左值和右值的区别 -【C++特性】左值引用、右值引用 - std::endl
右值引用会被清空
a,b都被清空
#include
#include
int main() {
std::vector&& a = { 1 };
std::vectorb = { 1 };
std::vectorc(std::move(a));
std::vectord(std::move(b));
}
2参数构造函数3 4 析构函数 移动构造函数class A
2参数构造函数3 4 析构函数 析构函数
a,b都没清空
#include 拼多多帮助力入口
#include
int main() {
std::vector&& a = { 1 };
std::vectorb = { 1 };
std::vectorc(a);
std::vectord(b);
}
a,b,e的类型相同
#include
#include
int main() {
std::vector&& a = { 1 };
std::vectorb = { 1 };
std::vector& e = b;
std::cout << typeid(a).name() << std::endl;
std::cout << typeid(b).name() << std::endl;
std::cout << typeid(e).name() << std::endl;
}
运行结果 class std::vector class std::vector class std::vector
局部变量:非引用和右值引用似乎没区别
#include
#include
class A {
public :
A(const int& a) {
std::cout << "执行拷贝构造函数\n";
}
A(const int&& a) {
std::cout << "执行移动构造函数\n";
}
};
int main() {
A a = 1;
A&& b = 1;
std::cout << "\n";
int x = 2;
A c = x;
A&& d = x;
}
执行移动构造函数 执行移动构造函数
执行拷贝构造函数 执行拷贝构造函数
函数参数类型也相同
#include
#include
class A {
public :
A(const int& a) {
std::cout << "拷贝构造函数"<< typeid(a).name() << "\n";
}
A(const int&& a) {
std::cout <<"移动构造函数"<< typeid(a).name() << "\n";
}
};
int main() {
A a = 1;
int x = 2;
A c = x;
}
移动构造函数int 拷贝构造函数int
左右值的引用都不调用构造函数
初始化
#include
#include
class A {
public:
A(const int& a) {
std::cout << "int拷贝构造函数" << typeid(a).name() << "\n";
}
A(const int&& a) {
std::cout << "int移动构造函数" << typeid(a).name() << "\n";
}
A(int a,int b) {
std::cout << "2参数构造函数" << a << " " << b << "\n";
}
A(const A& a) {
std::cout << "烤贝构造函数" << typeid(a).name() << "\n";
}
A(const A&& a) {
std::cout << "移动构造函数" << typeid(a).name() << "\n";
}
A& operator=(const A& a) {
std::cout << "=" << typeid(a).name() << "\n";
return *this;
}
A& operator=(const A&& a) {
std::cout << "移动=" << typeid(a).name() << "\n";
return *this;
}
~A() {
std::cout << "析构函数" << "\n";
}
};
int main() {
A a(3, 4);
A b = a;
std::cout << "===============================\n";
A& c = a;
std::cout << "===============================\n";
A&& d = A(5, 6);
}
运行结果。 #include #include
class A { public: A(const int& a) { std::cout << “int拷贝构造函数” << typeid(a).name() << “\n”; } A(const int&& a) { std::cout << “int移动构造函数” << typeid(a).name() << “\n”; } A(int a,int b) { std::cout << “2参数构造函数” << a << " " << b << “\n”; } A(const A& a) { std::cout << “烤贝构造函数” << typeid(a).name() << “\n”; } A(const A&& a) { std::cout << “移动构造函数” << typeid(a).name() << “\n”; } A& operator=(const A& a) { std::cout << “=” << typeid(a).name() << “\n”; return *this; } A& operator=(const A&& a) { std::cout << “移动=” << typeid(a).name() << “\n”; return *this; } ~A() { std::cout << “析构函数” << “\n”; } };
int main() { A a(3, 4); A b = a; std::cout << " ===============================\n"; A& c = a; std::cout << " ===============================\n"; A&& d = A(5, 6); }
函数返回非引用
A f() {
return A(3, 4);
}
int main() {
A b = f();
std::cout << " ====================================\n";
A&& d =f();
}
2参数构造函数3 4
2参数构造函数3 4 析构函数 析构函数
返回右值引用
A&& f() {
return A(3, 4);
}
int main() {
A b = f();
std::cout << " ====================================\n";
A&& d = f();
}
有问题,先执行析构函数,再执行拷贝构造函数。 2参数构造函数3 4 析构函数 移动构造函数class A
==================================== 2参数构造函数3 4 析构函数 析构函数
万能引用
普通函数参数,&&表示右值引用;模板函数的参数,&&表示万能引用,支持左值引用和有值引用。
void g(int& a) {
std::cout << "int&\n";
}
void g(int&& a) {
std::cout << "int&&\n";
}
templatevoid f(T&& a) {
g(std::forward(a));
}
int main() {
int a;
int& b = a;
int&& c = int(2);
std::cout << " ====================================\n";
f(a);
f(b);
f(c);
f(int());
}
运行结果。 如下修改后:
templatevoid f(T&& a) {
g(a);
}
==================================== int& int& int& int&
扩展阅读
计算几何为骨,排样优化为魂作品一:亲士套料工具箱 作品二: 亲士CAD工具箱经典文章推荐:二维排样万物皆数学查阅鄙人的博文,请点击博文下载学院导航活到老,学到老。明朝中后期,大约50%的进士能当上堂官(副部及更高);能当上堂官的举人只有十余人。子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
测试环境
操作系统:win7 开发环境: VS2019 C++17 或者 操作系统:win10 开发环境: VS2022 C++17 如无特殊说明,本算法用**C++**实现。,c左值和右值的区别