Search code examples
c++boostboost-graph

Why are the arguments to BGL functions separated by dots, instead of commas?


I am reading someone's codes. This is a function from boost graph library. This is the original function definition.

 void dijkstra_shortest_paths
      (const Graph& g,
       typename graph_traits<Graph>::vertex_descriptor s, 
       PredecessorMap predecessor, DistanceMap distance, WeightMap weight, 
       VertexIndexMap index_map,
       CompareFunction compare, CombineFunction combine, DistInf inf, DistZero zero,
       DijkstraVisitor vis, ColorMap color = default)

This is the piece of code I picked out from someone's. It works, but I just don't understand why he used dot in between predecessor_map weight_mapanddistance_map instead of comma? How many parameters he passed into the function?

dijkstra_shortest_paths(graph, source_vertex,
                          predecessor_map(&predecessors[0])
                          .weight_map(get(&Edge::cost, graph))
                          .distance_map(&distances[0]));

Solution

  • The documentation explains what's going on:

    Many of the Boost.Graph algorithms have a long list of parameters, most of which have default values. This causes several problems.

    [...]

    A better solution is provided by bgl_named_params. This class allows users to provide parameters is any order, and matches arguments to parameters based on parameter names.

    [...]

    Each of the arguments is passed to a function whose name indicates which parameter the argument is for. Each of the named parameters is separated by a period, not a comma.

    [...]

    Typically the user never needs to deal with the bgl_named_params class directly, since there are functions like boost::weight_map that create an instance of bgl_named_params.

    See also What is the “Named Parameter Idiom”?.