boost::containers 和错误:“C2679:二进制 '=':未找到运算符”

Kot*_*lot 4 boost visual-studio-2008 visual-c++

我正在尝试在 Visual Studio 2008 下编译以下代码:

struct test
{
    boost::container::vector<int> v1;
};
test v1, v3;
const test & v2 = v3;
v1 = v2;
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:
error C2679: binary '=' : no operator found which requires a right-hand operation of 'const test' (or there is no Acceptable conversion)
could be 'test &test::operator =(尝试匹配参数列表 '(test, const test)' 时测试 &)'

当使用普通 std::vector 而不是 boost::container 等效项时,代码会编译。我正在寻找为什么此代码无法编译以及如何使其编译的答案。

Kot*_*lot 5

我发现了一个已经被问过的类似问题: boost::container::vector failed to compile with C++03 compiler

我们观察到的行为似乎是为 boost 社区设计的,并且为 boost 社区所知: Boost::move 仿真限制章节“派生自或持有可复制和可移动类型的类中的赋值运算符”。

为了使主要问题中显示的代码起作用,必须使用 BOOST_COPYABLE_AND_MOVABLE 宏将类声明为可复制和可移动。还需要明确定义副本分配的 const 版本。C++03 编译器的更正代码版本:

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;
};
Run Code Online (Sandbox Code Playgroud)

那些额外的类装饰可能确实很烦人,尤其是当代码库很大时。我不想花时间浏览代码并添加赋值运算符。