{ { // Test 1; // 测试临时对象直接传值给临时对象; cout << "====== Test 1: test return temp value hold by temp value" << endl; A a = returnTempValue(); }
cout << endl;
{ // Test 2; // 测试临时对象直接传值给已存在的对象; cout << "====== Test 2: test return temp value hold by exist value" << endl; a = returnTempValue(); }
cout << endl;
{ // Test 3; // 测试临时对象直接传值给const引用; cout << "====== Test 3: test return temp value hold by const ref" << endl; const A &a = returnTempValue(); }
cout << endl; }
{ { // Test 4; // 测试已存在的对象直接传值给临时对象; cout << "====== Test 4: test return exist value hold by temp value" << endl; A a = returnExistValue(); }
cout << endl;
{ // Test 5; // 测试已存在的对象直接传值给已存在对象; cout << "====== Test 5: test return exist value hold by exist value" << endl; a = returnExistValue(); }
cout << endl;
{ // Test 6; // 测试已存在对象直接传值给const引用; cout << "====== Test 6: test return exist value hold by const ref" << endl; const A &a = returnExistValue(); }
cout << endl; }
{ { // Test 7; // 测试已存在的对象传const引用给临时对象; cout << "====== Test 7: test return exist value by const ref hold by temp value" << endl; A a = returnExistValueByConstRef(); }
cout << endl;
{ // Test 8; // 测试已存在的对象传const引用给已存在对象; cout << "====== Test 8: test return exist value by const ref hold by exist value" << endl; a = returnExistValueByConstRef(); }
cout << endl;
{ // Test 9; // 测试已存在对象传const引用给const引用; cout << "====== Test 9: test return exist value by const ref hold by const ref" << endl; const A &a = returnExistValueByConstRef(); } }
while (1); }
测试书结果
这里只给出Visual Studio 2017 release模式,g++ 4.8 -O0 -g模式以及g++ 4.8 -O3模式的输出,Visual Studio 2017 Debug模式一般不会直接用于生产环境,所以忽略 经过测试,Visual Studio 2017 Release 模式,g++ 4.8 -g -O0模式,g++ 4.8 -O3模式都输出如下内容
{ { // Test 1; // 测试临时对象直接传值给临时对象; // 编译器优化,不会有多余的copy操作; cout << "====== Test 1: test return temp value hold by temp value" << endl; A a = returnTempValue(); }
cout << endl;
{ // Test 2; // 测试临时对象直接传值给已存在的对象; // 如果对象定义了rvalue操作的话,消耗了一次move operator=操作,否则消耗一次copy操作; cout << "====== Test 2: test return temp value hold by exist value" << endl; a = returnTempValue(); }
cout << endl;
{ // Test 3; // 测试临时对象直接传值给const引用; // 同Test 1; cout << "====== Test 3: test return temp value hold by const ref" << endl; const A &a = returnTempValue(); }
cout << endl; }
{ { // Test 4; // 测试已存在的对象直接传值给临时对象; // 被赋值的临时对象会被copy构造; cout << "====== Test 4: test return exist value hold by temp value" << endl; A a = returnExistValue(); }
cout << endl;
{ // Test 5; // 测试已存在的对象直接传值给已存在对象; // 一次copy构造加一次move operator=操作; cout << "====== Test 5: test return exist value hold by exist value" << endl; a = returnExistValue(); }
cout << endl;
{ // Test 6; // 测试已存在对象直接传值给const引用; // 同Test 1; cout << "====== Test 6: test return exist value hold by const ref" << endl; const A &a = returnExistValue(); }
cout << endl; }
{ { // Test 7; // 测试已存在的对象传const引用给临时对象; // 消耗一次copy操作; cout << "====== Test 7: test return exist value by const ref hold by temp value" << endl; A a = returnExistValueByConstRef(); }
cout << endl;
{ // Test 8; // 测试已存在的对象传const引用给已存在对象; // 同Test 4; cout << "====== Test 8: test return exist value by const ref hold by exist value" << endl; a = returnExistValueByConstRef(); }
cout << endl;
{ // Test 9; // 测试已存在对象传const引用给const引用; // 无消耗; cout << "====== Test 9: test return exist value by const ref hold by const ref" << endl; const A &a = returnExistValueByConstRef(); } }