如何在最后 3 个运算符中强制执行 RVO:
#include <iostream>
class Noisy {
private:
int m_value;
public:
Noisy(int value = 0): m_value(value)
{
std::cout << "Noisy(int)\n";
}
Noisy(const Noisy& other): m_value(other.m_value)
{
std::cout << "Noisy(const Noisy&)\n";
}
Noisy(Noisy&& other): m_value(other.m_value)
{
std::cout << "Noisy(Noisy&&)\n";
}
//~Noisy() {
// std::cout << "dtor\n";
//}
Noisy operator+(const Noisy& rhs) &
{
std::cout << "+(const Noisy&)&\n";
return Noisy(m_value + rhs.m_value);
}
Noisy operator+(Noisy&& rhs) &
{
std::cout << "+(Noisy&&)&\n";
rhs.m_value += m_value;
return rhs; //std::move(rhs);
}
Noisy operator+(const Noisy& rhs) &&
{
std::cout << "+(const Noisy&) &&\n";
this->m_value += rhs.m_value;
return *this; //std::move(*this);
}
Noisy operator+(Noisy&& rhs) &&
{
std::cout << "+(Noisy&&) &&\n";
this->m_value += rhs.m_value;
return *this; //std::move(*this);
}
};
int main()
{
Noisy a, b, c, d, e, f, g;
Noisy z = a + b + c + d + e + f + g;
return 0;
}
程序运行输出:
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
+(const Noisy&)&
Noisy(int)
+(const Noisy&) &&
Noisy(const Noisy&)
+(const Noisy&) &&
Noisy(const Noisy&)
+(const Noisy&) &&
Noisy(const Noisy&)
+(const Noisy&) &&
Noisy(const Noisy&)
+(const Noisy&) &&
Noisy(const Noisy&)
或者std::move
在最后三个运算符中显式使用时:
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
+(const Noisy&)&
Noisy(int)
+(const Noisy&) &&
Noisy(Noisy&&)
+(const Noisy&) &&
Noisy(Noisy&&)
+(const Noisy&) &&
Noisy(Noisy&&)
+(const Noisy&) &&
Noisy(Noisy&&)
+(const Noisy&) &&
Noisy(Noisy&&)
我不想在运算符中复制,如下所示:
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
Noisy(int)
+(const Noisy&)&
Noisy(int)
+(const Noisy&) &&
+(const Noisy&) &&
+(const Noisy&) &&
+(const Noisy&) &&
+(const Noisy&) &&
到目前为止,我想出的唯一方法是从操作员那里返回一个引用,但这显然会导致一个悬空引用。
我用新鲜的 g++ 在 c++14 和 c++17 中编译。
更新
我知道在不违反规则的情况下,不可能强制编译器做我想做的事。
但是编译器在本地优化右值的 ptevents 是什么?我想它可以在第一次添加期间创建一个右值,在下一次添加中修改该右值,然后分配给结果变量。