dimanche 31 décembre 2017

Using boost move with an instance parameter

I have a function that deals with a movable-only instance that is a wrapper around something else. That object provides means of access to the wrapped object and some checks which require it to be non-copyable. (Use case is a table of values, where the dtor of the wrapper should assert that all values were were accessed)

I defined a custom ctor from the wrapped type and implemented the move ctor/assignment.

However I'm getting an error due to an attempted copy: error: 'Movable::Movable(Movable&)' is private within this context

It works fine in C++11 but I need portability to C++03. How can I do this without an explicit instantiation of the wrapper and moving it into the function?

MWE:

#include <boost/move/move.hpp>
#include <iostream>

class Movable{
    BOOST_MOVABLE_BUT_NOT_COPYABLE(Movable)
public:
    int i;
    Movable(int j):i(j){}
    Movable(BOOST_RV_REF(Movable) other) // Move constructor
        : i(boost::move(other.i))
    {}

    Movable& operator=(BOOST_RV_REF(Movable) other) // Move assignment
    {
        if(this != &other)
            i = boost::move(other.i);
        return *this;
    }
};

void bar(Movable mov){
    mov.i = 22;
    std::cout << mov.i;
}

int main(int argc, char* argv[])
{
    bar(5);
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire