c++ - BGL : bundled object without a default constructor?

标签 c++ boost

这个问题是关于Boost Graph Library的。

假设我的图表类型定义如下:

using Graph = boost::adjacency_list<vecS, setS, directedS, State>;

现在,如果我理解正确的话,我可以向图中添加一个新的 State 对象 s,如下所示:

auto vd = add_vertex(g); // g is a Graph object
g[vd] = s;

这种方式有两个问题:

  • 它要求 State 有一个默认构造函数。对于这个特定的类 State 来说,默认构造函数根本没有意义。

  • 这样,我就可以在添加顶点时支付 State 对象的默认构造费用。该成本是纯粹的运行时开销。

那么,有没有办法避免捆绑对象需要默认构造函数?

最佳答案

documentation :

The types of all property values must be Copy Constructible, Assignable, and Default Constructible. The property maps obtained from the adjacency_list class are models of the Lvalue Property Map concept. If the adjacency_list is const, then the property map is constant, otherwise the property map is mutable.

您应该创建一个能够保存引用您的状态的属性类型。

对于第一步,您可能会成功使用采用该属性的重载:

auto vd = add_vertex(s, g); // g is a MutablePropertyGraph object
g[vd] = s;

查看其他docs

关于c++ - BGL : bundled object without a default constructor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33810464/

相关文章:

c++ - 调用 pIBidiSpl2::SendRecvXMLString 时出错

c++ - 制作一个关于 qtopia 错误的文件

c++ - 如何创建具有雕刻效果的文本?

c++ - 从字符串指针获取字符串内容C++

c++ - boost 线程池和进程之间的互斥量

c++ - int i = i ^ i 的值;它总是零或未定义的行为吗?

c++ - Boost.Process - 如何让一个进程运行一个函数?

c++ - 使用 Qt 和 Boost 为应用程序编译 CppUTest 测试时的奇怪行为

c++ - 需要解释以理解 asio REFERENCE_COUNTED 示例

c++ - boost::atomic 的 header 是什么