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