64,637
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <iostream>
using namespace std;
int g_constructCount = 0;
int g_copyConstructCount = 0;
int g_destructCount = 0;
struct A
{
A() { cout << "construct: " << ++g_constructCount << endl; }
A(const A& a) { cout << "copy construct: " << ++g_copyConstructCount << endl; }
~A() { cout << "destruct: " << ++g_destructCount << endl; }
void Output() { cout << "A" << endl; }
};
A GetA(int iType)
{
if (iType == 0)
{
A a1;
return a1;
}
else
{
A a2;
return a2;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
A&& a = GetA(0);
a.Output();
return 0;
}
#include "stdafx.h"
#include <iostream>
using namespace std;
int g_constructCount = 0;
int g_copyConstructCount = 0;
int g_destructCount = 0;
struct A
{
A() { cout << "construct: " << ++g_constructCount << endl; }
A(const A& a) { cout << "copy construct: " << ++g_copyConstructCount << endl; }
~A() { cout << "destruct: " << ++g_destructCount << endl; }
void Output() { cout << "A" << endl; }
};
A GetA(int iType)
{
if (iType == 0)
{
A a1;
return a1;
}
else
{
A a2;
return a2;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
A a = GetA(0);
a.Output();
return 0;
}
看以上的代码,输出的结果与
A&& a = GetA(0);
是一样的,如果return的地方有一个拷贝构造,那构造A a对象时,从函数返回值到生成a,是否该再一次调用拷贝构造?但结果却是只调用了一次。[/quote]返回值优化[/quote]
看来是了,VS2013是否有设置关闭NVO?
我本来以为通过右值引用,不会发生拷贝构造,看来一次拷贝构造是省不了了。[/quote]
拷贝构造不是通过右值引用省掉的,要用 copy ellision 来省掉。
不过 copy ellision 现在还不是强制的。A&& GetA(int iType)
{
A a;
return std::move(a);
}
这种代码是错的A&& GetA(int iType)
{
A a;
return std::move(a);
}
[/quote]
这么改,就变为调用A类的Move构造函数了。
我是想通过由值引用延伸函数内部局部变量的生命周期,看来是不行了。
#include "stdafx.h"
#include <iostream>
using namespace std;
int g_constructCount = 0;
int g_copyConstructCount = 0;
int g_destructCount = 0;
struct A
{
A() { cout << "construct: " << ++g_constructCount << endl; }
A(const A& a) { cout << "copy construct: " << ++g_copyConstructCount << endl; }
~A() { cout << "destruct: " << ++g_destructCount << endl; }
void Output() { cout << "A" << endl; }
};
A GetA(int iType)
{
if (iType == 0)
{
A a1;
return a1;
}
else
{
A a2;
return a2;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
A a = GetA(0);
a.Output();
return 0;
}
看以上的代码,输出的结果与
A&& a = GetA(0);
是一样的,如果return的地方有一个拷贝构造,那构造A a对象时,从函数返回值到生成a,是否该再一次调用拷贝构造?但结果却是只调用了一次。[/quote]返回值优化[/quote]
看来是了,VS2013是否有设置关闭NVO?
我本来以为通过右值引用,不会发生拷贝构造,看来一次拷贝构造是省不了了。#include "stdafx.h"
#include <iostream>
using namespace std;
int g_constructCount = 0;
int g_copyConstructCount = 0;
int g_destructCount = 0;
struct A
{
A() { cout << "construct: " << ++g_constructCount << endl; }
A(const A& a) { cout << "copy construct: " << ++g_copyConstructCount << endl; }
~A() { cout << "destruct: " << ++g_destructCount << endl; }
void Output() { cout << "A" << endl; }
};
A GetA(int iType)
{
if (iType == 0)
{
A a1;
return a1;
}
else
{
A a2;
return a2;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
A a = GetA(0);
a.Output();
return 0;
}
看以上的代码,输出的结果与
A&& a = GetA(0);
是一样的,如果return的地方有一个拷贝构造,那构造A a对象时,从函数返回值到生成a,是否该再一次调用拷贝构造?但结果却是只调用了一次。[/quote]返回值优化#include "stdafx.h"
#include <iostream>
using namespace std;
int g_constructCount = 0;
int g_copyConstructCount = 0;
int g_destructCount = 0;
struct A
{
A() { cout << "construct: " << ++g_constructCount << endl; }
A(const A& a) { cout << "copy construct: " << ++g_copyConstructCount << endl; }
~A() { cout << "destruct: " << ++g_destructCount << endl; }
void Output() { cout << "A" << endl; }
};
A GetA(int iType)
{
if (iType == 0)
{
A a1;
return a1;
}
else
{
A a2;
return a2;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
A a = GetA(0);
a.Output();
return 0;
}
看以上的代码,输出的结果与
A&& a = GetA(0);
是一样的,如果return的地方有一个拷贝构造,那构造A a对象时,从函数返回值到生成a,是否该再一次调用拷贝构造?但结果却是只调用了一次。