忽略候选模板:推断类型与调整后的类型

时间:2018-01-23 10:09:37

标签: c++ templates boost graph

在boost图库中remove_edge的函数调用存在一个非常奇怪的问题。

当我在main函数中调用它时,编译和运行时都可以; 但是当我在模板函数test_remove_edge中调用它时,我得到了编译错误。

代码示例和编译错误消息在这里。

#include <iostream>
#include <vector>
#include <string>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/max_cardinality_matching.hpp>
#include <boost/graph/maximum_weighted_matching.hpp>

using namespace boost;

template <typename Graph>
void test_remove_edge(const Graph& g)
{
   typedef typename graph_traits<Graph>::edge_iterator edge_iterator_t;

   edge_iterator_t ei, ei_end;
   for (boost::tie(ei,ei_end) = edges(g); ei != ei_end; ++ei)
   {
       std::cout << typeid(*ei).name() << ", " << typeid(g).name() << std::endl; // exactly same with the one in main
       remove_edge(*ei, g); // compile error, see message pasted below
   }
}

int main(int argc, const char * argv[])
{
    typedef property<edge_weight_t, float, property<edge_index_t, int>> EdgeProperty;
    typedef adjacency_list<vecS, vecS, undirectedS, no_property, EdgeProperty> my_graph;

    const int n_vertices = 8;

    my_graph g(n_vertices);

    add_edge(1,2,EdgeProperty(5),g);
    add_edge(0,4,EdgeProperty(1),g);
    add_edge(1,5,EdgeProperty(4),g);
    add_edge(2,6,EdgeProperty(1),g);
    add_edge(3,7,EdgeProperty(4),g);

   typedef typename graph_traits<my_graph>::edge_iterator edge_iterator_t;
   edge_iterator_t ei, ei_end;
   for (boost::tie(ei,ei_end) = edges(g); ei != ei_end; ++ei)
   {
       std::cout << typeid(*ei).name() << ", " << typeid(g).name() << std::endl; // exactly same with the one in test_remove_edge
       remove_edge(*ei, g); // compile ok, runtime ok
   }

    test_remove_edge(g);

    return 0;
}

编译错误消息:

  

忽略了候选模板:推断类型

     

&#39; undirected_graph_helper&gt;,   boost :: no_property,boost :: listS&gt ;, boost :: vecS,boost :: vecS,   boost :: undirectedS,boost :: no_property,   boost :: property&gt;,   boost :: no_property,boost :: listS&gt; :: config&gt; &安培;&#39;

     第二个参数的

与调整后的类型

不匹配      

&#39; const boost :: adjacency_list&gt;,   boost :: no_property,boost :: listS&gt;&#39;

     参数

[与EdgeOrIter =   提高::详细:: edge_desc_impl,   配置=   boost :: detail :: adj_list_gen&gt;,   boost :: no_property,boost :: listS&gt ;, boost :: vecS,boost :: vecS,   boost :: undirectedS,boost :: no_property,   boost :: property&gt;,   boost :: no_property,boost :: listS&gt; :: config]

我确信编译器会为两种情况选择相同的重载函数remove_edge(Xcode告诉我)。另外我知道通过检查typeid(T).name()的输出,调用remove_edge时参数类型是相同的。

感到绝望,非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

remove_edge修改图形,因此无法在const-reference上调用它。最简单的解决方法:

template <typename Graph> void test_remove_edge(Graph& g) {
相关问题