当类包含boost::container::flat_set时,复制对象时出错
Error copying objects when class contains boost::container::flat_set
在(false?)认为boost::container::flat_set
是std::set
的替代品的情况下,我将set
替换为flat_set
,只要我希望元素数量少,并且搜索性能比插入更关键。
在后来的阶段,我被一个令人困惑的编译错误难住了,我最终将其追溯到使用flat_set
作为类成员。
例如:
class Room {
private:
boost::container::flat_set<int> v;
};
下面的代码不会编译,但如果我用std::set
替换flat_set
,它就可以正常工作。
Room a;
Room b = Room(); // Example 1. Compiles OK
a = b; // Example 2. Compiles OK
a = Room(); // Example 3. Eeeek! Compile fails on this line
我看到的编译错误是:
错误:"a=Room()"中的"operator="不匹配注:候选人为:注意:房间和房间::operator=(房间和)注意:参数1没有从"Room"到"Room&"的已知转换
我的问题是:
- 这是预期的错误吗?如果是,那么我该如何解决它
- 示例代码中的三个语句有何不同,为什么只有最后一个语句失败
- 为什么编译器抱怨
Room
的赋值运算符而不是flat_set
?flat_set
的使用是否影响了为类生成的默认运算符
完整的样本程序:
#include <boost/container/flat_set.hpp>
class Room {
private:
boost::container::flat_set<int> v;
};
int main(int argc, char *argv[]) {
Room a;
Room b = Room();
a = b;
a = Room(); // compilation fails here
return 0;
};
这是Boost.move执行的移动模拟的已知限制。您可以在此处找到有关它的更多信息:http://www.boost.org/doc/libs/1_52_0/doc/html/move/emulation_limitations.html#move.emulation_limitations.assignment_operator
免责声明: 我是OP;我已经接受了K-ballo的回答,因为它导致了最终的解决方案,我发布这篇文章是为了用一些细节来补充这个帖子
正如在接受的答案中所提到的,这确实是对使用BOOST_COPYABLE_AND_MOVABLE宏的类的已知限制(这适用于boost::container
中的许多类,包括flat_set
和flat_map
)。
为了克服这一点,我为类定义了一个赋值运算符,该运算符采用常量引用参数。例如,在quesiton中的示例Room
类的情况下,它将沿着以下行:
class Room {
private:
boost::container::flat_set<int> v;
public:
Room& operator=(const Room& source) {
v = source.v;
return *this;
}
};
相关文章:
- 将文本从一个文件复制到另一个c++流时出错
- 不可复制的对象、映射和shared_ptr:复制构造函数中出错
- 当类包含boost::container::flat_set时,复制对象时出错
- 在C++中复制矢量时,矢量运算符出错=
- 具有std::原子成员变量的类的复制构造函数/赋值运算符出错
- 使用复制构造函数时出错
- 使用不可复制(但可移动)键移动地图分配时出错
- 使用 C++11 复制构造 boost::shared_ptr 时出错
- 在 C++ 中编写复制构造函数时出错
- 复制构造函数出错
- 使用 ifsream 和字符串流复制二进制文件时出错
- 仅在发布版本中出错,从 std::vector 复制结构
- 将数据复制到iplimage时出错
- 结构初始化中的隐式复制构造函数出错
- 在模板化类的复制构造函数中使用默认值时出错
- 将字符串复制到二维字符数组时出错
- 复制构造函数中的std::copy出错
- 使用 rdbuf() 和运算符<<在 c++ 中复制流时出错
- 使用默认复制构造函数时出错:"deleted function"
- 将矢量bool复制到CUDA内存时出错