Boost Tutorials

Using Named Parameters

If you've done any serious work with graph libraries, you've most likely written function calls with long lists of unreadable arguments, like the following:
prim_mst(out_g, in_g, w_map, s, d_map, i_map, std::less<double>(), 1.0e+308, 0.0);

The Boost Parameter Library enables you to write functions that also accept arguments by name so that their purpose is much clearer:

prim_mst(output_graph = out_g, input_graph = in_g, weight_map = w_map,
 root_vertex = s, distance_map = d_map, vertex_id_map = i_map,
 comparator = std::less<double>(), infinity = 1.0e+308, zero = 0.0);

You can omit arguments that have useful default values (normally illegal in C++ for global or free functions):

prim_mst(output_graph = out_g, input_graph = in_g, weight_map = w_map);

You can alter the order of your arguments to your liking as long as you name them:

prim_mst(input_graph = in_g, weight_map = w_map, output_graph = out_g);

You can even mix positional arguments with named ones, if you so choose:

prim_mst(out_g, in_g, weight_map = w_map);

(The one restriction is that you must put all your positional arguments before your named ones, so use this particular feature with care.)

Each function in this library that accepts named arguments comes with a version that takes in an Argument Pack. For our purposes, this is a comma-delimited list of named arguments enclosed in an extra pair of parentheses. With this scheme, the following function calls are equivalent:

 prim_mst(output_graph = out_g, input_graph = in_g, weight_map = w_map);
 prim_mst_with_named_params((output_graph = out_g, input_graph = in_g, weight_map = w_map));
 

This feature is useful if you have a class that models a certain concept and whose member function(s) can accept named arguments, and you need to encapsulate instances of it in another class, one whose member function(s) forward most of their arguments to the encapsulated object(s).

Using Boost.Random

If you feel enslaved by std::rand(), rejoice! The Boost Random Number Library offers a host of random number generators--as well as the ability to plug in your own--for randomizing mazes. Typical usage:
 typedef boost::mt19937 RNGEngine;  // Or choose another random number generator engine.
 typedef boost::uniform_int<> Distribution;  // Or choose another random distribution.
 RNGEngine base;  // Source of randomness.
 Distribution range(1, 20)  // Distribution that constrains the results to the specified number range [1,6].
 boost::variate_generator<RNGEngine&,Distribution> roll(base, range);  // Glues source of randomness with distribution.
 int r = roll();  // Rolls a 20-sided die (dice is plural).
 

This library binds itself to concepts rather than to actual classes or class templates; as long as you conform to these concepts, you can mix and match until you get the randomness you desire.


Multi-State Mazes in C++ is hosted by SourceForge.net. Use the Table of Contents for navigation.