C++11中std::move、std::forward、左右值引用、移动构造,哪个是长尾词的移动构造函数?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1891个文字,预计阅读时间需要8分钟。
关于C++11新特性中的std::move、std::forward以及左右值引用,网上资料已经很多了。我主要想针对测试性能做一个测试,整理一下相关逻辑。
首先,左值比较熟悉,右值则是临时变量或即将被销毁的变量。使用std::move和std::forward可以更高效地处理右值。
1. 使用std::move将左值临时化,然后以右值的形式传递,提高性能。
2.使用std::forward直接传递右值引用,避免不必要的拷贝或移动。
3.左值引用和右值引用的区别在于,左值引用绑定到已有的左值,而右值引用绑定到右值。
下面是一个简单的测试示例:
cpp
#include #includetemplatevoid process(T&& arg) { // 假设这里是处理数据的逻辑}
int main() { auto start=std::chrono::high_resolution_clock::now();
// 测试std::move int a=5; process(std::move(a)); // 临时化a,提高性能
// 测试std::forward int b=6; process(std::forward(b)); // 直接传递b的右值引用
auto end=std::chrono::high_resolution_clock::now(); std::chrono::duration elapsed=end - start; std::cout << Elapsed time: < return 0;} 这个示例通过测试std::move和std::forward的性能,可以了解它们在实际应用中的效果。 关于C++11新特性之std::move、std::forward、左右值引用网上资料已经很多了,我主要针对测试性能做一个测试,梳理一下这些逻辑,首先,左值比较熟悉,右值就是临时变量,意味着使用一次就不会再被使用了。针对这两种值引入了左值引用和右值引用,以及引用折叠的概念。 1.右值引用的举例测试
#include <iostream>
using namespace std;
//创建一个测试类
class A
{
public:
A() : m_a(55)
{
}
int m_a;
};
void funcA(A&& param) // 右值引用参数,只接受右值
{
cout << param.m_a << endl; // param与a的地址一致,仅仅只是取了一个新名字
}
int main()
{
A a;
funcA(move(a)); //必须将其转换为右值
cout << a.m_a << endl; //正常打印,所以std::move并没有移动的能力
return 0;
}
2.左值和右值引用的举例测试,以及引出万能引用 构造一组重载函数,分别接受右值,和左值的参数,还有const A&的参数重载函数。
void funcA(const A& param)//既可以接受右值引用,也可以接受左值引用,但是有一个隐式转换const A&
void funcA(A& param)// 接受左值引用
void funcA(A&& param) // 接受右值引用
const A& param既可以接受右值引用,也可以接受左值引用,但是存在一个隐式转换,const使用受限制。
#include <iostream>
using namespace std;
//创建一个测试类
class A
{
public:
A() : m_a(55) // 构造函数
{
cout << "Constructor" << endl;
}
A(const A & other) : m_a(55) // copy构造函数
{
cout << "Copy Constructor" << endl;
if (this == &other)
{
return;
}
this->m_a = other.m_a;
}
A& operator=(const A& other) // 赋值构造函数
{
cout << "= Constructor" << endl;
if (this == &other)
{
return *this;
}
this->m_a = other.m_a;
return *this;
}
int m_a;
};
void test(A&& pa) //测试是否为右值
{
cout << "只接受右值" << endl;
}
void funcA(const A& param) // 既可以接受右值引用,也可以接受左值引用,但是有一个隐式转换const A&
{
//test(param); //编译不过,param可以接受右值,但是param被转换为const左值
//test(std::forward<A>(param)); //编译不过,param可以接受右值,但是param被转换为const左值
cout << param.m_a << endl;
}
void funcA(A& param) // 接受左值引用
{
//test(param); //编译不过,param可以接受右值,但是param被转换为左值
test(std::forward<A>(param)); //编译通过,通过forward转发
cout << param.m_a << endl;
}
void funcA(A&& param) // 接受右值引用
{
//test(param); //编译不过,param被转换为左值
test(std::forward<A>(param)); //编译通过,通过forward转发
cout << param.m_a << endl;
}
int main()
{
A a;
const A& b = a;
funcA(a);
funcA(move(a));
funcA(b);
cout << a.m_a << endl; //正常打印,所以std::move并没有移动的能力
return 0;
}
对此C++11引入了万能引用的概念,使得不需要那么多的重载函数,既可以接受右值引用,也可以接受左值引用。但是函数内部,再需要调用一个左值或者右值的函数时,我们就得需要forward模版类。
#include <iostream> using namespace std; //创建一个测试类 class A { public: A() : m_a(new int(55)) // 构造函数 { cout << "Constructor" << endl; } A(const A & other) : m_a(new int(55)) // copy构造函数 { cout << "Copy Constructor" << endl; if (this == &other) return; this->m_a = other.m_a; } A& operator=(const A& other) // 赋值构造函数 { cout << "= Constructor" << endl; if (this == &other) return *this; this->m_a = other.m_a; return *this; } int* m_a; }; void test(A&& pa) //测试是否为右值 { cout << "只接受右值" << endl; } void test(A& pa) //测试是否为左值 { cout << "只接受左值" << endl; } template<class T> void funcA(T&& param) { test(std::forward<T>(param)); //编译通过,通过forward完美转发 cout << *param.m_a << endl; } int main() { A a; funcA(a); funcA(move(a)); cout << *a.m_a << endl; //正常打印,所以std::move并没有移动的能力 return 0; }
3.移动构造函数的引出
以上的所有特性,所能体现出来的是我们对于临时变量的使用,尽可能的使用中间生成的临时变量,提高性能,所谓的榨取最后的性能。移动构造函数注意的两点
1.调用移动构造函数时参数(被移动者)必须是右值。
2.调用移动构造函数后被移动者就不能再被使用。
#include <iostream> using namespace std; //创建一个测试类 class A { public: A() : m_a(new int(55)) // 构造函数 { cout << "Constructor" << endl; } A(const A & other) : m_a(new int(55)) // copy构造函数 { cout << "Copy Constructor" << endl; if (this == &other) { return; } this->m_a = other.m_a; } A& operator=(const A& other) // 赋值构造函数 { cout << "= Constructor" << endl; if (this == &other) { return *this; } this->m_a = other.m_a; return *this; } A(A&& other) : m_a(other.m_a) // 移动构造函数,参数是一个右值, { cout << "Move Constructor" << endl; if (this == &other) { return; } other.m_a = nullptr; //移动后将被移动的对象数据清空 } int* m_a; }; void test(A&& pa) //测试是否为右值 { cout << "只接受右值" << endl; } void test(A& pa) //测试是否为左值 { cout << "只接受左值" << endl; } template<class T> void funcA(T&& param) { test(std::forward<T>(param)); //编译通过,通过forward完美转发 cout << *param.m_a << endl; } int main() { A a; funcA(a); funcA(move(a)); A b(move(a)); //调用移动构造函数,新的对象是b对象 cout << *a.m_a << endl; //数据已被移动,程序崩溃 return 0; }
移动构造函数一定程度上较少了临时内存的申请,减少不必要的拷贝,节省了空间和时间。以上特性在使用中还有很多需要注意的地方,如果我遇到了会及时的添加到这里,分享给大家,一起加油。
到此这篇关于C++11中std::move、std::forward、左右值引用、移动构造函数的测试问题的文章就介绍到这了,更多相关C++11中std::move、std 内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
本文共计1891个文字,预计阅读时间需要8分钟。
关于C++11新特性中的std::move、std::forward以及左右值引用,网上资料已经很多了。我主要想针对测试性能做一个测试,整理一下相关逻辑。
首先,左值比较熟悉,右值则是临时变量或即将被销毁的变量。使用std::move和std::forward可以更高效地处理右值。
1. 使用std::move将左值临时化,然后以右值的形式传递,提高性能。
2.使用std::forward直接传递右值引用,避免不必要的拷贝或移动。
3.左值引用和右值引用的区别在于,左值引用绑定到已有的左值,而右值引用绑定到右值。
下面是一个简单的测试示例:
cpp
#include #includetemplatevoid process(T&& arg) { // 假设这里是处理数据的逻辑}
int main() { auto start=std::chrono::high_resolution_clock::now();
// 测试std::move int a=5; process(std::move(a)); // 临时化a,提高性能
// 测试std::forward int b=6; process(std::forward(b)); // 直接传递b的右值引用
auto end=std::chrono::high_resolution_clock::now(); std::chrono::duration elapsed=end - start; std::cout << Elapsed time: < return 0;} 这个示例通过测试std::move和std::forward的性能,可以了解它们在实际应用中的效果。 关于C++11新特性之std::move、std::forward、左右值引用网上资料已经很多了,我主要针对测试性能做一个测试,梳理一下这些逻辑,首先,左值比较熟悉,右值就是临时变量,意味着使用一次就不会再被使用了。针对这两种值引入了左值引用和右值引用,以及引用折叠的概念。 1.右值引用的举例测试
#include <iostream>
using namespace std;
//创建一个测试类
class A
{
public:
A() : m_a(55)
{
}
int m_a;
};
void funcA(A&& param) // 右值引用参数,只接受右值
{
cout << param.m_a << endl; // param与a的地址一致,仅仅只是取了一个新名字
}
int main()
{
A a;
funcA(move(a)); //必须将其转换为右值
cout << a.m_a << endl; //正常打印,所以std::move并没有移动的能力
return 0;
}
2.左值和右值引用的举例测试,以及引出万能引用 构造一组重载函数,分别接受右值,和左值的参数,还有const A&的参数重载函数。
void funcA(const A& param)//既可以接受右值引用,也可以接受左值引用,但是有一个隐式转换const A&
void funcA(A& param)// 接受左值引用
void funcA(A&& param) // 接受右值引用
const A& param既可以接受右值引用,也可以接受左值引用,但是存在一个隐式转换,const使用受限制。
#include <iostream>
using namespace std;
//创建一个测试类
class A
{
public:
A() : m_a(55) // 构造函数
{
cout << "Constructor" << endl;
}
A(const A & other) : m_a(55) // copy构造函数
{
cout << "Copy Constructor" << endl;
if (this == &other)
{
return;
}
this->m_a = other.m_a;
}
A& operator=(const A& other) // 赋值构造函数
{
cout << "= Constructor" << endl;
if (this == &other)
{
return *this;
}
this->m_a = other.m_a;
return *this;
}
int m_a;
};
void test(A&& pa) //测试是否为右值
{
cout << "只接受右值" << endl;
}
void funcA(const A& param) // 既可以接受右值引用,也可以接受左值引用,但是有一个隐式转换const A&
{
//test(param); //编译不过,param可以接受右值,但是param被转换为const左值
//test(std::forward<A>(param)); //编译不过,param可以接受右值,但是param被转换为const左值
cout << param.m_a << endl;
}
void funcA(A& param) // 接受左值引用
{
//test(param); //编译不过,param可以接受右值,但是param被转换为左值
test(std::forward<A>(param)); //编译通过,通过forward转发
cout << param.m_a << endl;
}
void funcA(A&& param) // 接受右值引用
{
//test(param); //编译不过,param被转换为左值
test(std::forward<A>(param)); //编译通过,通过forward转发
cout << param.m_a << endl;
}
int main()
{
A a;
const A& b = a;
funcA(a);
funcA(move(a));
funcA(b);
cout << a.m_a << endl; //正常打印,所以std::move并没有移动的能力
return 0;
}
对此C++11引入了万能引用的概念,使得不需要那么多的重载函数,既可以接受右值引用,也可以接受左值引用。但是函数内部,再需要调用一个左值或者右值的函数时,我们就得需要forward模版类。
#include <iostream> using namespace std; //创建一个测试类 class A { public: A() : m_a(new int(55)) // 构造函数 { cout << "Constructor" << endl; } A(const A & other) : m_a(new int(55)) // copy构造函数 { cout << "Copy Constructor" << endl; if (this == &other) return; this->m_a = other.m_a; } A& operator=(const A& other) // 赋值构造函数 { cout << "= Constructor" << endl; if (this == &other) return *this; this->m_a = other.m_a; return *this; } int* m_a; }; void test(A&& pa) //测试是否为右值 { cout << "只接受右值" << endl; } void test(A& pa) //测试是否为左值 { cout << "只接受左值" << endl; } template<class T> void funcA(T&& param) { test(std::forward<T>(param)); //编译通过,通过forward完美转发 cout << *param.m_a << endl; } int main() { A a; funcA(a); funcA(move(a)); cout << *a.m_a << endl; //正常打印,所以std::move并没有移动的能力 return 0; }
3.移动构造函数的引出
以上的所有特性,所能体现出来的是我们对于临时变量的使用,尽可能的使用中间生成的临时变量,提高性能,所谓的榨取最后的性能。移动构造函数注意的两点
1.调用移动构造函数时参数(被移动者)必须是右值。
2.调用移动构造函数后被移动者就不能再被使用。
#include <iostream> using namespace std; //创建一个测试类 class A { public: A() : m_a(new int(55)) // 构造函数 { cout << "Constructor" << endl; } A(const A & other) : m_a(new int(55)) // copy构造函数 { cout << "Copy Constructor" << endl; if (this == &other) { return; } this->m_a = other.m_a; } A& operator=(const A& other) // 赋值构造函数 { cout << "= Constructor" << endl; if (this == &other) { return *this; } this->m_a = other.m_a; return *this; } A(A&& other) : m_a(other.m_a) // 移动构造函数,参数是一个右值, { cout << "Move Constructor" << endl; if (this == &other) { return; } other.m_a = nullptr; //移动后将被移动的对象数据清空 } int* m_a; }; void test(A&& pa) //测试是否为右值 { cout << "只接受右值" << endl; } void test(A& pa) //测试是否为左值 { cout << "只接受左值" << endl; } template<class T> void funcA(T&& param) { test(std::forward<T>(param)); //编译通过,通过forward完美转发 cout << *param.m_a << endl; } int main() { A a; funcA(a); funcA(move(a)); A b(move(a)); //调用移动构造函数,新的对象是b对象 cout << *a.m_a << endl; //数据已被移动,程序崩溃 return 0; }
移动构造函数一定程度上较少了临时内存的申请,减少不必要的拷贝,节省了空间和时间。以上特性在使用中还有很多需要注意的地方,如果我遇到了会及时的添加到这里,分享给大家,一起加油。
到此这篇关于C++11中std::move、std::forward、左右值引用、移动构造函数的测试问题的文章就介绍到这了,更多相关C++11中std::move、std 内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

