bgl_named_params.html 3.2 KB

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