简体   繁体   English

与dynamic_properties和write_graphviz有关的boost :: graph编译问题

[英]boost::graph compilation issue with dynamic_properties and write_graphviz

This question is about boost::graph and how to handle properties associated to vertices (and/or edges). 这个问题是关于boost :: graph以及如何处理与顶点(和/或边)相关的属性的。 I am quite confused about handling this, but I suspect it could be a template-related issue. 对于处理此问题,我感到很困惑,但是我怀疑这可能是与模板相关的问题。

Let's say I have this graph definition: 假设我有以下图形定义:

struct myVertex_t {
    int color;
};

typedef boost::adjacency_list<
    boost::vecS,                   // edge container
    boost::vecS,                   // vertex container
    boost::undirectedS,            // type of graph
    myVertex_t,                    // vertex properties
    boost::property<               // edge properties
        boost::edge_color_t,             // ???
        boost::default_color_type        // enum, holds 5 colors
    >
> myGraph_t;

AFAIK, this way of storing properties for vertices is called " bundle properties " and seems to be a third way of storing this information, although it is said in the manual that: AFAIK,这种存储顶点属性的方法称为“ 束属性 ”,似乎是存储此信息的第三种方法,尽管在手册中说:

There are two kinds of graph properties: interior and exterior. 图形属性有两种:内部和外部。

Back to my main question. 回到我的主要问题。 Now, I can instanciate and printout a graph using the "dot" format this way: 现在,我可以使用“点”格式实例化并打印出图形:

 int main()
 {
    myGraph_t g;
    boost::add_edge(0, 1, g);

    boost::dynamic_properties dp;
    dp.property("color",   boost::get( &myVertex_t::color,  g ) );
    dp.property("node_id", boost::get( boost::vertex_index, g ) );
    boost::write_graphviz_dp( std::cout , g, dp);
 }

Online here 在这里在线

This is based on this answer in a similar question, and compiles fine. 这是基于类似问题中的答案 ,并且可以编译。

Now I want to separate the printing in a separate function, so I write the same code in a templated function, just replacing concrete types with template type arguments: 现在,我想将打印分为一个单独的函数,因此我在模板化函数中编写了相同的代码,只是将具体类型替换为模板类型参数:

template<typename graph_t, typename vertex_t>
void RenderGraph( const graph_t& g )
{
    boost::dynamic_properties dp;
    dp.property( "color",   boost::get( &vertex_t::color,    g ) );
    dp.property( "node_id", boost::get( boost::vertex_index, g ) );
    boost::write_graphviz_dp( std::cout, g, dp );
}

int main()
{
    myGraph_t g;
    boost::add_edge(0, 1, g);

    RenderGraph<myGraph_t,myVertex_t>( g );
}

But this does not compile : 但这不能编译

property_map.hpp:361:44: error: assignment of read-only location ... property_map.hpp:361:44:错误:分配只读位置...

Any ideas what I did wrong ? 有任何想法我做错了吗?

property_map.hpp:361:44: error: assignment of read-only location ... property_map.hpp:361:44:错误:分配只读位置...

Yeah, sadly the fact that g is const there makes the default property factory function illegal. 是的,可悲的事实是g在那里存在,使默认property工厂功能非法。 Dynamic properties are constructed in a writable way if the model allows it: 如果模型允许,则以可写方式构造动态属性:

Requirements: PropertyMap must model Readable Property Map or Read/Write Property Map. 要求: PropertyMap必须对可读属性图或读/写属性图建模。

Because the property map is writable, the dynamic property compiles the writing branch too. 因为属性映射是可写的,所以动态属性也会编译写作分支。

You'd have to take the argument as non-const or manually override the Property traits for the underlying maps (see the comments here ( Cut set of a graph, Boost Graph Library ) for an example). 您必须将参数设为非常量,或手动覆盖基础地图的“属性”特性(例如,请参见此处的注释(示例的图形切割集,Boost Graph Library )。

You might consider reporting this as a usability issue, as logically, the properties should be const there . 您可能会考虑将其报告为可用性问题,因为从逻辑上讲,属性应该在那里const

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM