Move from temporary used in method chaining

孤街浪徒 提交于 2019-12-11 08:37:14

问题


I am trying to do something similar to this:

#include <vector>
#include <memory>

struct Bar
    {
    Bar& doThings()
        {return *this;}

    std::unique_ptr<int> m_content; // A non-copyable type
    };

struct Foo
    {
    Foo& append(Bar&& obj)
        {
        objects.push_back(std::move(obj));
        return *this;
        }

    std::vector<Bar> objects;
    };

int test()
    {
    Foo test;
    test.append(std::move(Bar{}.doThings())) //Ok
    // Not ok
      .append(Bar{}.doThings())
        ;
    }

error: cannot bind rvalue reference of type Bar&& to lvalue of type Bar

Is it possible to make this work without the explicit std::move?

Trying to overload doThings does not solve the problem:

error: Bar&& Bar::doThings() && cannot be overloaded


回答1:


You can add ref-qualified overloads of doThings():

struct Bar
    {
    Bar& doThings() &
        {return *this;}

    Bar&& doThings() &&
        {return std::move(*this);}

    std::unique_ptr<int> m_content; // A non-copyable type
    };



回答2:


The problem is that when you return the instance from the function, you don't have an rvalue.

However, there is a way to overload functions based on the rvalue/lvalue of the object:

 Bar& doThings() & {return *this;}
 Bar doThings() && {return std::move(*this); }


来源:https://stackoverflow.com/questions/51216397/move-from-temporary-used-in-method-chaining

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!