首页 > 解决方案 > 是否有合理的替代重载和完美转发?

问题描述

如果我通过复制/移动接受一个值然后对其执行移动,它似乎复制了 LValues 并移动了 RValues。

这段代码在这两种情况下都能正确有效地执行吗?

它是为 useA2() 创建 RValue 和 LValue 重载或将 useA2 函数转换为模板以使用转发的合理替代方案吗?

struct A
{
    int *buff;
    A() { cout << "A::constructor\n"; buff = new int[1000]; } //expensive 
    A(const A& a) { cout << "A::copy constructor\n";   buff = new int[1000]; memcpy(buff, a.buff, 1000); }
    A(A&& a) { cout << "A::move constructor\n";   buff = a.buff; a.buff = nullptr; }
    ~A() { cout << "A::destructor\n"; delete buff; }
};


A getA()
{
    A temp;  // without temp, compiler can perform copy elision, skipping copy/move constructors
    return temp;
}


void useA2(A a)
{
    A a1 = std::move(a);
}


void demo()
{
    A a1;
    //useA2(getA());  // performs 2 moves
    useA2(a1);   // performs a copy to the input param, then moves the copy 

}

标签: c++c++11rvalue-referenceperfect-forwarding

解决方案


可能最通用和最有效的解决方案是使用完美转发

template <typename... Ts>
void useA2(Ts&&... vs)
{
   A a1 { std::forward<Ts>(vs)... };
}

然后,useA2(a1);将只调用一个复制构造函数,而无需任何不必要的移动。


推荐阅读