Search code examples
c++boostboost-graph

why <> is empty in adjacency_list<>


Can I ask what the <> is for in adjacency_list<> ? I am new to stl. I know I can define a container like this: vector<int> vec, but why here it is empty in <>? Thank you.

#include <boost/graph/adjacency_list.hpp>
    using namespace boost;
    adjacency_list<> g;
    // adds four vertices to the graph
    adjacency_list<>::vertex_descriptor v1 = add_vertex(g);
    adjacency_list<>::vertex_descriptor v2 = add_vertex(g);
    adjacency_list<>::vertex_descriptor v3 = add_vertex(g);
    adjacency_list<>::vertex_descriptor v4 = add_vertex(g);

Solution

  • It's because adjacency_list is a templated type. You must specify <> when using C++ templates.

    The full definition for the type is:

    template <class OutEdgeListS = vecS,
              class VertexListS = vecS,
              class DirectedS = directedS,
              class VertexProperty = no_property,
              class EdgeProperty = no_property,
              class GraphProperty = no_property,
              class EdgeListS = listS>
    class adjacency_list
    {
        ...
    }
    

    Notice that each template parameter has a default: vecS, vecS, directedS, no_property, no_property, no_property, listS, respectively.

    The empty <> means you want the default classes for the template parameters. By not specifying the specific values for the template parameters, you get the defaults.

    The reason <> is needed, and can't be left out (which would be nice, yes) is because of the way the C++ language has been defined. You can avoid it by using a typedef, but ultimately the angle brackets are required for using template types.