Search code examples
c++movemethod-chainingtemporary-objects

Move from temporary used in method chaining


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


Solution

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