Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

C++ Boost

bgl_named_params<Param, Type, Rest>

Many of the Boost.Graph algorithms have a long list of parameters, most of which have default values. This causes several problems. First, C++ does not provide a mechanism for handling default parameters of template functions. However, this can be overcome by creating multiply version of an algorithm with different numbers of parameters with each version providing defaults for some subset of the parameters. This is the approach used in previous versions of Boost.Graph. This solution is still unsatisfactory for several reasons:

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.

The following code shows an example of calling bellman_ford_shortest_paths using the named parameter technique. 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.

  bool r = boost::bellman_ford_shortest_paths(g, int(N),
     boost::weight_map(weight).
     distance_map(&distance[0]).
     predecessor_map(&parent[0]));

The order in which the arguments are provided does not matter as long as they are matched with the correct parameter function. Here is an call to bellman_ford_shortest_paths that is equivalent to the one above.

  bool r = boost::bellman_ford_shortest_paths(g, int(N),
     boost::predecessor_map(&parent[0]).
     distance_map(&distance[0]).
     weight_map(weight));

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.


Copyright © 2000-2001 Jeremy Siek, Indiana University (jsiek@osl.iu.edu)
Lie-Quan Lee, Indiana University (llee@cs.indiana.edu)
Andrew Lumsdaine, Indiana University (lums@osl.iu.edu)