【问题标题】:How do I prevent code repeat between rvalue and lvalue member functions?如何防止右值和左值成员函数之间的代码重复?
【发布时间】:2016-03-13 15:19:57
【问题描述】:

对于下面的程序代码,我必须在接收右值和左值引用的一对成员函数中编写相同的代码。

我的目标是只使用一对中的一个(例如;只使用接受右值的那些),以及其他的。看了std::forward的参考资料,据我了解,好像是为了这个目的。但是,当我删除左值引用时,会出现以下编译器错误。

'TestClass::TestClass(const TestClass &)': 无法将参数 1 从 'std::wstring' 转换为 'std::wstring &&'

如何防止这种代码重复?

#include <iostream>
#include <string>

class TestClass
{
    public:
        TestClass(const std::wstring    &  Text)
            : Text(Text)
        {
            std::wcout << L"LValue Constructor : " << Text << std::endl;
            /*Some code here...*/
        }
        TestClass(      std::wstring    && Text)
            : Text(std::forward<std::wstring>(Text))
        {
            std::wcout << L"RValue Constructor : " << this->Text << std::endl;
            /*Same code here...*/
        }
        TestClass(const TestClass       &  Another)
            : Text(Another.Text)
        {
            std::wcout << L"Copy   Constructor : " << Text << std::endl;
            /*Some code here...*/
        }
        TestClass(      TestClass       && Another)
            : Text(std::forward<std::wstring>(Another.Text))
        {
            std::wcout << L"Move   Constructor : " << Text << std::endl;
            /*Same code here...*/
        }

    private:
        std::wstring Text;
};

int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
{
    std::wstring Argument(L"Testing Copy");
    TestClass Class1Copy(Argument);
    TestClass Class1Move(L"Testing Move");
    TestClass Class2Copy(Class1Copy);
    TestClass Class2Move(std::move(Class1Move));

    _wsystem(L"pause");
    return 0;
}

输出:

LValue Constructor : Testing Copy  
RValue Constructor : Testing Move  
Copy   Constructor : Testing Copy  
Move   Constructor : Testing Move  
Press any key to continue . . .

【问题讨论】:

  • Text(std::forward&lt;std::wstring&gt;(Text)) 应该是 Text(std::move(Text))。在您使用forward 而不是move 的其他地方也是如此。

标签: c++ c++11 move-semantics rvalue-reference move-constructor


【解决方案1】:

如果预计移动构造非常便宜,您可以按价值取值并从价值中移动。与一对复制和移动重载相比,这恰好多移动 1 次。

如果您希望获得最佳效率,和/或如果移动构造更便宜但又便宜足以忽略,您可以转发:

template<class T>
std::decay_t<T> copy(T&& t) {
  return std::forward<T>(t);
}
class TestClass {
public:
  TestClass(std::wstring const&  Text)
    TestClass( copy(Text) )
  {}
  TestClass(TestClass const& o)
    : TestClass( o.Text )
  {}
  TestClass(TestClass&& o)
    : TestClass( std::move(o).Text ) // pattern does the right thing more often than `std::move(o.Text)` does.
  {}
  // only "real" ctor:
  TestClass( std::wstring&& Text)
    : Text(std::forward<std::wstring>(Text))
  {
    std::wcout << L"RValue Constructor : " << this->Text << std::endl;
    /*Code here...*/
  }
// ...

现在一切都转发给一个构造函数。

您甚至可以混合使用这两种技术:对std::wstring 使用按值(我们知道这很便宜)并为TestClass 代码(或任何不太可能稳定的东西)进行转发.

【讨论】:

    【解决方案2】:

    您可以按价值然后move。那你只需要N重载,不需要2N

    TestClass(std::wstring Text)
        : Text(std::move(Text))
    {
    }
    

    你可以通过什么都不写来避免复制构造函数和移动构造函数的重复;在这种情况下,编译器会默认生成它们。

    【讨论】:

      【解决方案3】:

      我认为你不能这样做,因为函数的签名决定了它们何时何地被使用。就像copy constructorassignment operator。他们做了有点类似的事情,但编译器会根据上下文调用适当的事情。

      如果您想避免重复使用代码,只需将共性分解到单独的函数中即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多