motivation.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Motivation</title>
  5. <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
  7. <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Boost.Optional">
  8. <link rel="up" href="../../optional/tutorial.html" title="Tutorial">
  9. <link rel="prev" href="../../optional/tutorial.html" title="Tutorial">
  10. <link rel="next" href="design_overview.html" title="Design Overview">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="../../optional/tutorial.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../optional/tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="design_overview.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h3 class="title">
  27. <a name="boost_optional.tutorial.motivation"></a><a class="link" href="motivation.html" title="Motivation">Motivation</a>
  28. </h3></div></div></div>
  29. <p>
  30. Consider these functions which should return a value but which might not
  31. have a value to return:
  32. </p>
  33. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  34. <li class="listitem">
  35. (A) <code class="computeroutput"><span class="keyword">double</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="keyword">double</span> <span class="identifier">n</span> <span class="special">);</span></code>
  36. </li>
  37. <li class="listitem">
  38. (B) <code class="computeroutput"><span class="keyword">char</span> <span class="identifier">get_async_input</span><span class="special">();</span></code>
  39. </li>
  40. <li class="listitem">
  41. (C) <code class="computeroutput"><span class="identifier">point</span> <span class="identifier">polygon</span><span class="special">::</span><span class="identifier">get_any_point_effectively_inside</span><span class="special">();</span></code>
  42. </li>
  43. </ul></div>
  44. <p>
  45. There are different approaches to the issue of not having a value to return.
  46. </p>
  47. <p>
  48. A typical approach is to consider the existence of a valid return value as
  49. a postcondition, so that if the function cannot compute the value to return,
  50. it has either undefined behavior (and can use assert in a debug build) or
  51. uses a runtime check and throws an exception if the postcondition is violated.
  52. This is a reasonable choice for example, for function (A), because the lack
  53. of a proper return value is directly related to an invalid parameter (out
  54. of domain argument), so it is appropriate to require the callee to supply
  55. only parameters in a valid domain for execution to continue normally.
  56. </p>
  57. <p>
  58. However, function (B), because of its asynchronous nature, does not fail
  59. just because it can't find a value to return; so it is incorrect to consider
  60. such a situation an error and assert or throw an exception. This function
  61. must return, and somehow, must tell the callee that it is not returning a
  62. meaningful value.
  63. </p>
  64. <p>
  65. A similar situation occurs with function (C): it is conceptually an error
  66. to ask a <span class="emphasis"><em>null-area</em></span> polygon to return a point inside
  67. itself, but in many applications, it is just impractical for performance
  68. reasons to treat this as an error (because detecting that the polygon has
  69. no area might be too expensive to be required to be tested previously), and
  70. either an arbitrary point (typically at infinity) is returned, or some efficient
  71. way to tell the callee that there is no such point is used.
  72. </p>
  73. <p>
  74. There are various mechanisms to let functions communicate that the returned
  75. value is not valid. One such mechanism, which is quite common since it has
  76. zero or negligible overhead, is to use a special value which is reserved
  77. to communicate this. Classical examples of such special values are <code class="computeroutput"><span class="identifier">EOF</span></code>, <code class="computeroutput"><span class="identifier">string</span><span class="special">::</span><span class="identifier">npos</span></code>,
  78. points at infinity, etc...
  79. </p>
  80. <p>
  81. When those values exist, i.e. the return type can hold all meaningful values
  82. <span class="emphasis"><em>plus</em></span> the <span class="emphasis"><em>signal</em></span> value, this mechanism
  83. is quite appropriate and well known. Unfortunately, there are cases when
  84. such values do not exist. In these cases, the usual alternative is either
  85. to use a wider type, such as <code class="computeroutput"><span class="keyword">int</span></code>
  86. in place of <code class="computeroutput"><span class="keyword">char</span></code>; or a compound
  87. type, such as <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">point</span><span class="special">,</span><span class="keyword">bool</span><span class="special">&gt;</span></code>.
  88. </p>
  89. <p>
  90. Returning a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span><span class="keyword">bool</span><span class="special">&gt;</span></code>, thus attaching a boolean flag to the
  91. result which indicates if the result is meaningful, has the advantage that
  92. can be turned into a consistent idiom since the first element of the pair
  93. can be whatever the function would conceptually return. For example, the
  94. last two functions could have the following interface:
  95. </p>
  96. <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span><span class="keyword">bool</span><span class="special">&gt;</span> <span class="identifier">get_async_input</span><span class="special">();</span>
  97. <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">point</span><span class="special">,</span><span class="keyword">bool</span><span class="special">&gt;</span> <span class="identifier">polygon</span><span class="special">::</span><span class="identifier">get_any_point_effectively_inside</span><span class="special">();</span>
  98. </pre>
  99. <p>
  100. These functions use a consistent interface for dealing with possibly nonexistent
  101. results:
  102. </p>
  103. <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">point</span><span class="special">,</span><span class="keyword">bool</span><span class="special">&gt;</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">poly</span><span class="special">.</span><span class="identifier">get_any_point_effectively_inside</span><span class="special">();</span>
  104. <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">second</span> <span class="special">)</span>
  105. <span class="identifier">flood_fill</span><span class="special">(</span><span class="identifier">p</span><span class="special">.</span><span class="identifier">first</span><span class="special">);</span>
  106. </pre>
  107. <p>
  108. However, not only is this quite a burden syntactically, it is also error
  109. prone since the user can easily use the function result (first element of
  110. the pair) without ever checking if it has a valid value.
  111. </p>
  112. <p>
  113. Clearly, we need a better idiom.
  114. </p>
  115. </div>
  116. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  117. <td align="left"></td>
  118. <td align="right"><div class="copyright-footer">Copyright &#169; 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright &#169; 2014 Andrzej Krzemie&#324;ski<p>
  119. Distributed under the Boost Software License, Version 1.0. (See accompanying
  120. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  121. </p>
  122. </div></td>
  123. </tr></table>
  124. <hr>
  125. <div class="spirit-nav">
  126. <a accesskey="p" href="../../optional/tutorial.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../optional/tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="design_overview.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  127. </div>
  128. </body>
  129. </html>