【问题标题】:Compile error while moving typedef into function template argument将 typedef 移动到函数模板参数时编译错误
【发布时间】:2013-01-27 01:53:02
【问题描述】:

在尝试将一些 boost 图形代码重构为函数模板时,我遇到了一些编译错误。

我正在尝试将 using NODE_TYPE = int; 行移动到函数模板参数中。

我做错了什么?

编译错误

Test_Dijkstra_Graph.cpp:48:28: error: expected type-specifier
Test_Dijkstra_Graph.cpp:48:28: error: expected ‘;’
Test_Dijkstra_Graph.cpp:49:28: error: expected type-specifier
Test_Dijkstra_Graph.cpp:49:28: error: expected ‘;’

OLD CODE 编译良好​​strong>

void 
find_shortest_paths_by_pairs()
{
  using NODE_TYPE         = int;
  using EDGE              = std::pair<NODE_TYPE,NODE_TYPE>;
  using GRAPH_T           = adjacency_list< 
        listS, vecS, directedS, no_property, property< edge_weight_t, NODE_TYPE >>;
  using vertex_descriptor = graph_traits<GRAPH_T>::vertex_descriptor;
  using edge_descriptor   = graph_traits<GRAPH_T>::edge_descriptor;
  // ...
}

NEW CODE 编译失败

template<typename NODE_TYPE>
void find_shortest_paths_by_pairs()
{
  using EDGE              = std::pair<NODE_TYPE,NODE_TYPE>;
  using GRAPH_T           = adjacency_list< 
        listS, vecS, directedS, no_property, property< edge_weight_t, NODE_TYPE >>;
  using vertex_descriptor = graph_traits<GRAPH_T>::vertex_descriptor;   // ERROR HERE
  using edge_descriptor   = graph_traits<GRAPH_T>::edge_descriptor;     // ERROR HERE
  // ...
}

// find_shortest_paths_by_pairs<int>();

【问题讨论】:

    标签: c++ templates c++11 typedef


    【解决方案1】:

    NODE_TYPE 导致 GRAPH_T 成为依赖类型,因此您需要一些 typenames:

    using vertex_descriptor = typename graph_traits<GRAPH_T>::vertex_descriptor;
    using edge_descriptor   = typename graph_traits<GRAPH_T>::edge_descriptor;
    

    (Comeau 有一个常见问题解答,我通常会链接到此处,详细说明为什么这是必要的,但似乎他们已经让他们的域名过期了。:-[)

    编辑:嘿,Comeau 的域名又回来了。说FAQ:http://www.comeaucomputing.com/techtalk/templates/#typename

    【讨论】:

    • @Johannes:我不这么认为,因为我听说过 Comeau 最近发布了 C++11 的传言。但是他们的网站肯定已经死了——很遗憾,因为他们的在线编译器是测试 C++03 代码的中流砥柱。 :-[
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 2011-06-29
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    相关资源
    最近更新 更多