我在读想要速度?在C++ Next 博客上通过值传递并创建了这个程序来感受 C++0x 中的复制省略和移动语义:
#include <vector>
#include <iostream>
class MoveableClass {
public:
MoveableClass() : m_simpleData(0), instance(++Instances) {
std::cout << "Construct instance " << instance << " (no data)" << std::endl;
}
MoveableClass(std::vector<double> data) : m_data(std::move(data)), m_simpleData(0), instance(++Instances) {
std::cout << "Construct instance " << instance << " (with data)" << std::endl;
}
MoveableClass(int simpleData) : m_simpleData(simpleData), instance(++Instances) {
std::cout << "Construct instance " << instance << " (with simple data)" << std::endl;
}
MoveableClass(const MoveableClass& other)
: m_data(other.m_data), m_simpleData(other.m_simpleData), instance(++Instances)
{
std::cout << "Construct instance " << instance << " from a copy of " << other.instance << std::endl;
Elided = false;
}
MoveableClass(MoveableClass&& other)
: m_data(std::move(other.m_data)), m_simpleData(other.m_simpleData), instance(++Instances)
{
std::cout << "Construct instance " << instance << " from a move of " << other.instance << std::endl;
Elided = false;
}
MoveableClass& operator=(MoveableClass other) {
std::cout << "Assign to instance " << instance << " from " << other.instance << std::endl;
other.Swap(*this);
return *this;
}
~MoveableClass() {
std::cout << "Destroy instance " << instance << std::endl;
--Instances;
}
void Swap(MoveableClass& other) {
std::swap(m_data, other.m_data);
std::swap(m_simpleData, other.m_simpleData);
}
static int Instances;
static bool Elided;
private:
int instance;
int m_simpleData;
std::vector<double> m_data;
};
int MoveableClass::Instances = 0;
bool MoveableClass::Elided = true;
std::vector<double> BunchOfData() {
return std::vector<double>(9999999);
}
int SimpleData() {
return 9999999;
}
MoveableClass CreateRVO() {
return MoveableClass(BunchOfData());
}
MoveableClass CreateNRVO() {
MoveableClass named(BunchOfData());
return named;
}
MoveableClass CreateRVO_Simple() {
return MoveableClass(SimpleData());
}
MoveableClass CreateNRVO_Simple() {
MoveableClass named(SimpleData());
return named;
}
int main(int argc, char* argv[]) {
std::cout << "\nMove assign from RVO: " << '\n';
{
MoveableClass a;
a = CreateRVO();
}
std::cout << "Move elided: " << (MoveableClass::Elided ? "Yes" : "No") << '\n';
MoveableClass::Elided = true; // reset for next test
std::cout << "\nMove assign from RVO simple: " << '\n';
{
MoveableClass a;
a = CreateRVO_Simple();
}
std::cout << "Move elided: " << (MoveableClass::Elided ? "Yes" : "No") << '\n';
MoveableClass::Elided = true; // reset for next test
std::cout << "\nMove assign from NRVO: " << '\n';
{
MoveableClass a;
a = CreateNRVO();
}
std::cout << "Move elided: " << (MoveableClass::Elided ? "Yes" : "No") << '\n';
MoveableClass::Elided = true; // reset for next test
std::cout << "\nMove assign from NRVO simple: " << std::endl;
{
MoveableClass a;
a = CreateNRVO_Simple();
}
std::cout << "Move elided: " << (MoveableClass::Elided ? "Yes" : "No") << '\n';
MoveableClass::Elided = true; // reset for next test
}
这是我在 Visual C++ 10.0(Beta 2)上以发布模式编译时得到的输出:
从 RVO 移动分配:
构造实例 1(无数据)
构造实例 2(有数据)
从移动 2 构造实例 3
销毁实例 2
从 3 分配给实例 1
销毁实例 3
销毁实例 1
移动省略:否从 RVO 简单移动分配:
构造实例 1(无数据)
构造实例 2(使用简单数据)
从 2 分配给实例 1
销毁实例 2
销毁实例 1
移动省略:是从 NRVO 移动分配:
构造实例 1(无数据)
构造实例 2(有数据)
从 2 分配给实例 1
销毁实例 2
销毁实例 1
移动省略:是从 NRVO 简单移动分配:
构造实例 1(无数据)
构造实例 2(使用简单数据)
从 2 分配给实例 1
销毁实例 2
销毁实例 1
移动省略:是
但是,我对一件事感到困惑。如您所见,除了第一个动作之外,所有动作都被省略了。为什么编译器不能在第 86 行使用 MoveableClass(std::vector) 执行 RVO,但可以在第 97 行使用 MoveableClass(int)?这只是 MSVC 的错误还是有充分的理由?如果有充分的理由,为什么它仍然可以在第 91 行对 MoveableClass(std::vector) 执行 NRVO?
我想了解它,这样我才能快乐地入睡。:)