Move from temporary used in method chaining

2019-08-06 04:54发布

问题:

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); }