10_motivation.qbk 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. [/
  2. Boost.Optional
  3. Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal
  4. Copyright (c) 2014 Andrzej Krzemienski
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. ]
  9. [section Motivation]
  10. Consider these functions which should return a value but which might not have
  11. a value to return:
  12. * (A) `double sqrt(double n );`
  13. * (B) `char get_async_input();`
  14. * (C) `point polygon::get_any_point_effectively_inside();`
  15. There are different approaches to the issue of not having a value to return.
  16. A typical approach is to consider the existence of a valid return value as a
  17. postcondition, so that if the function cannot compute the value to return, it
  18. has either undefined behavior (and can use assert in a debug build) or uses a
  19. runtime check and throws an exception if the postcondition is violated. This
  20. is a reasonable choice for example, for function (A), because the lack of a
  21. proper return value is directly related to an invalid parameter (out of domain
  22. argument), so it is appropriate to require the callee to supply only parameters
  23. in a valid domain for execution to continue normally.
  24. However, function (B), because of its asynchronous nature, does not fail just
  25. because it can't find a value to return; so it is incorrect to consider such
  26. a situation an error and assert or throw an exception. This function must
  27. return, and somehow, must tell the callee that it is not returning a meaningful
  28. value.
  29. A similar situation occurs with function (C): it is conceptually an error to
  30. ask a ['null-area] polygon to return a point inside itself, but in many
  31. applications, it is just impractical for performance reasons to treat this as
  32. an error (because detecting that the polygon has no area might be too expensive
  33. to be required to be tested previously), and either an arbitrary point
  34. (typically at infinity) is returned, or some efficient way to tell the callee
  35. that there is no such point is used.
  36. There are various mechanisms to let functions communicate that the returned
  37. value is not valid. One such mechanism, which is quite common since it has
  38. zero or negligible overhead, is to use a special value which is reserved to
  39. communicate this. Classical examples of such special values are `EOF`,
  40. `string::npos`, points at infinity, etc...
  41. When those values exist, i.e. the return type can hold all meaningful values
  42. ['plus] the ['signal] value, this mechanism is quite appropriate and well known.
  43. Unfortunately, there are cases when such values do not exist. In these cases,
  44. the usual alternative is either to use a wider type, such as `int` in place of
  45. `char`; or a compound type, such as `std::pair<point,bool>`.
  46. Returning a `std::pair<T,bool>`, thus attaching a boolean flag to the result
  47. which indicates if the result is meaningful, has the advantage that can be
  48. turned into a consistent idiom since the first element of the pair can be
  49. whatever the function would conceptually return. For example, the last two
  50. functions could have the following interface:
  51. std::pair<char,bool> get_async_input();
  52. std::pair<point,bool> polygon::get_any_point_effectively_inside();
  53. These functions use a consistent interface for dealing with possibly nonexistent
  54. results:
  55. std::pair<point,bool> p = poly.get_any_point_effectively_inside();
  56. if ( p.second )
  57. flood_fill(p.first);
  58. However, not only is this quite a burden syntactically, it is also error prone
  59. since the user can easily use the function result (first element of the pair)
  60. without ever checking if it has a valid value.
  61. Clearly, we need a better idiom.
  62. [endsect]