KotBerbelot
KotBerbelot

Reputation: 153

boost::containers and error: "C2679: binary '=': no operator found"

I'm trying to compile the following pice of code under Visual Studio 2008:

struct test
{
    boost::container::vector<int> v1;
};
test v1, v3;
const test & v2 = v3;
v1 = v2;

The error I'm getting is:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const test' (or there is no acceptable conversion)
could be 'test &test::operator =(test &)' while trying to match the argument list '(test, const test)'

When normal std::vector is used instead of boost::container equivalent, code compiles. I'm looking for an answer why this code doesn't compile and how to make it compile.

Upvotes: 4

Views: 956

Answers (1)

KotBerbelot
KotBerbelot

Reputation: 153

I've found a similar question that already has been asked: boost::container::vector fails to compile with C++03 compiler

It seems that the behaviour we are observing is designed and known to the boost community: Boost::move emulation limitations chapter "Assignment operator in classes derived from or holding copyable and movable types".

In order to make the code shown in the main question to work one must declare class as copyable and movable using BOOST_COPYABLE_AND_MOVABLE macro. Also const version of the copy assignment needs to be defined explicitly. Corrected version of the code for C++03 compiler:

class test
{
private:
    BOOST_COPYABLE_AND_MOVABLE( test );
public:
    test& operator=(BOOST_COPY_ASSIGN_REF(test) p) // Copy assignment
    {
        v1 = p.v1;
        return *this;
    }
    boost::container::vector<int> v1;
};

Those additional class decorations may indeed become annoying especially when the codebase is large. Crawling through the code and adding assign operators is not a thing I would like to spend my time on.

Upvotes: 5

Related Questions