1 | <HTML> |
---|
2 | <!-- |
---|
3 | -- Copyright (c) Jeremy Siek 2000 |
---|
4 | -- |
---|
5 | -- Permission to use, copy, modify, distribute and sell this software |
---|
6 | -- and its documentation for any purpose is hereby granted without fee, |
---|
7 | -- provided that the above copyright notice appears in all copies and |
---|
8 | -- that both that copyright notice and this permission notice appear |
---|
9 | -- in supporting documentation. Jeremy Siek makes no |
---|
10 | -- representations about the suitability of this software for any |
---|
11 | -- purpose. It is provided "as is" without express or implied warranty. |
---|
12 | --> |
---|
13 | <Head> |
---|
14 | <Title>Boost Graph Library: Named Parameters</Title> |
---|
15 | <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" |
---|
16 | ALINK="#ff0000"> |
---|
17 | <IMG SRC="../../../boost.png" |
---|
18 | ALT="C++ Boost" width="277" height="86"> |
---|
19 | |
---|
20 | <BR Clear> |
---|
21 | |
---|
22 | <H1><A NAME="sec:bgl-named-params"></A> |
---|
23 | <pre> |
---|
24 | bgl_named_params<Param, Type, Rest> |
---|
25 | </pre> |
---|
26 | </H1> |
---|
27 | |
---|
28 | <p> |
---|
29 | Many of the Boost.Graph algorithms have a long list of parameters, |
---|
30 | most of which have default values. This causes several problems. |
---|
31 | First, C++ does not provide a mechanism for handling default |
---|
32 | parameters of template functions. However, this can be overcome by |
---|
33 | creating multiply version of an algorithm with different numbers of |
---|
34 | parameters with each version providing defaults for some subset of |
---|
35 | the parameters. This is the approach used in previous versions of |
---|
36 | Boost.Graph. This solution is still unsatisfactory for several |
---|
37 | reasons: |
---|
38 | |
---|
39 | <ul> |
---|
40 | <li>The defaults for parameters can only been used in a particular |
---|
41 | order. If the ordering of the defaults does not fit the users situation |
---|
42 | he or she has to resort to providing all the parameters. |
---|
43 | |
---|
44 | <li>Since the list of parameters is long, it is easy to forget |
---|
45 | the ordering. |
---|
46 | </ul> |
---|
47 | |
---|
48 | <p> |
---|
49 | A better solution is provided by <tt>bgl_named_params</tt>. This class |
---|
50 | allows users to provide parameters is any order, and matches |
---|
51 | arguments to parameters based on parameter names. |
---|
52 | |
---|
53 | <p> |
---|
54 | The following code shows an example of calling |
---|
55 | <tt>bellman_ford_shortest_paths</tt> using the named parameter |
---|
56 | technique. Each of the arguments is passed to a function whose name |
---|
57 | indicates which parameter the argument is for. Each of the named |
---|
58 | parameters is separated by a <b>period</b>, not a comma. |
---|
59 | |
---|
60 | <pre> |
---|
61 | bool r = boost::bellman_ford_shortest_paths(g, int(N), |
---|
62 | boost::weight_map(weight). |
---|
63 | distance_map(&distance[0]). |
---|
64 | predecessor_map(&parent[0])); |
---|
65 | </pre> |
---|
66 | |
---|
67 | <p>The order in which the arguments are provided does not matter as |
---|
68 | long as they are matched with the correct parameter function. Here is |
---|
69 | an call to <tt>bellman_ford_shortest_paths</tt> that is equivalent to |
---|
70 | the one above. |
---|
71 | |
---|
72 | <pre> |
---|
73 | bool r = boost::bellman_ford_shortest_paths(g, int(N), |
---|
74 | boost::predecessor_map(&parent[0]). |
---|
75 | distance_map(&distance[0]). |
---|
76 | weight_map(weight)); |
---|
77 | </pre> |
---|
78 | |
---|
79 | <p>Typically the user never needs to deal with the |
---|
80 | <tt>bgl_named_params</tt> class directly, since there are functions |
---|
81 | like <tt>boost::weight_map</tt> that create an instance of |
---|
82 | <tt>bgl_named_params</tt>. |
---|
83 | |
---|
84 | |
---|
85 | <br> |
---|
86 | <HR> |
---|
87 | <TABLE> |
---|
88 | <TR valign=top> |
---|
89 | <TD nowrap>Copyright © 2000-2001</TD><TD> |
---|
90 | <A HREF="../../../people/jeremy_siek.htm">Jeremy Siek</A>, |
---|
91 | Indiana University (<A |
---|
92 | HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>)<br> |
---|
93 | <A HREF="../../../people/liequan_lee.htm">Lie-Quan Lee</A>, Indiana University (<A HREF="mailto:llee@cs.indiana.edu">llee@cs.indiana.edu</A>)<br> |
---|
94 | <A HREF=http://www.osl.iu.edu/~lums>Andrew Lumsdaine</A>, |
---|
95 | Indiana University (<A |
---|
96 | HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>) |
---|
97 | </TD></TR></TABLE> |
---|
98 | |
---|
99 | </BODY> |
---|
100 | </HTML> |
---|