kfmfe04
kfmfe04

Reputation: 15327

Compile error while moving typedef into function template argument

In trying to refactor some boost graph code into a function template, I am running into some compile errors.

I am trying to move the using NODE_TYPE = int; line into a function template argument.

What am I doing wrong?

COMPILE ERROR

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 compiles fine

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 fails to compile

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>();

Upvotes: 1

Views: 104

Answers (1)

ildjarn
ildjarn

Reputation: 62975

NODE_TYPE causes GRAPH_T to be a dependent type, so you'll need a few typenames:

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

(There was a FAQ by Comeau that I would usually link to here giving more details on why this is necessary, but it seems they've let their domain name expire. :-[)

EDIT: Hey, Comeau's domain is back. Said FAQ: http://www.comeaucomputing.com/techtalk/templates/#typename

Upvotes: 5

Related Questions