17_gotchas.qbk 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. [section Gotchas]
  2. [section A note about optional<bool>]
  3. `optional<bool>` should be used with special caution and consideration.
  4. First, it is functionally similar to a tristate boolean (false, maybe, true)
  5. —such as __BOOST_TRIBOOL__— except that in a tristate boolean, the maybe state
  6. [_represents a valid value], unlike the corresponding state of an uninitialized
  7. `optional<bool>`.
  8. It should be carefully considered if an `optional<bool>` instead of a `tribool`
  9. is really needed.
  10. Second, although `optional<>` provides a contextual conversion to `bool` in C++11,
  11. this falls back to an implicit conversion on older compilers. This conversion refers
  12. to the initialization state and not to the contained value. Using `optional<bool>`
  13. can lead to subtle errors due to the implicit `bool` conversion:
  14. void foo ( bool v ) ;
  15. void bar()
  16. {
  17. optional<bool> v = try();
  18. // The following intended to pass the value of 'v' to foo():
  19. foo(v);
  20. // But instead, the initialization state is passed
  21. // due to a typo: it should have been foo(*v).
  22. }
  23. The only implicit conversion is to `bool`, and it is safe in the sense that
  24. typical integral promotions don't apply (i.e. if `foo()` takes an `int`
  25. instead, it won't compile).
  26. Third, mixed comparisons with `bool` work differently than similar mixed comparisons between pointers and `bool`, so the results might surprise you:
  27. optional<bool> oEmpty(none), oTrue(true), oFalse(false);
  28. if (oEmpty == none); // renders true
  29. if (oEmpty == false); // renders false!
  30. if (oEmpty == true); // renders false!
  31. if (oFalse == none); // renders false
  32. if (oFalse == false); // renders true!
  33. if (oFalse == true); // renders false
  34. if (oTrue == none); // renders false
  35. if (oTrue == false); // renders false
  36. if (oTrue == true); // renders true
  37. In other words, for `optional<>`, the following assertion does not hold:
  38. assert((opt == false) == (!opt));
  39. [endsect]
  40. [section Moved-from `optional`]
  41. When an optional object that contains a value is moved from (is a source of move constructor or assignment) it still contains a value and its contained value is left in a moved-from state. This can be illustrated with the following example.
  42. optional<std::unique_ptr<int>> opi {std::make_unique<int>(1)};
  43. optional<std::unique_ptr<int>> opj = std::move(opi);
  44. assert (opi);
  45. assert (*opi == nullptr);
  46. Quite a lot of people expect that when an object that contains a value is moved from, its contained value should be destroyed. This is not so, for performance reasons. Current semantics allow the implementation of `boost::opiotnal<T>` to be trivially copyable when `T` is trivial.
  47. [endsect]
  48. [section Mixed relational comparisons]
  49. Because `T` is convertible to `optional<T>` and because `opiotnal<T>` is __SGI_LESS_THAN_COMPARABLE__ when `T` is __SGI_LESS_THAN_COMPARABLE__,
  50. you can sometimes get an unexpected runtime result where you would rather expect a compiler error:
  51. optional<double> Flight_plan::weight(); // sometimes no weight can be returned
  52. bool is_aircraft_too_heavy(Flight_plan const& p)
  53. {
  54. return p.weight() > p.aircraft().max_weight(); // compiles!
  55. } // returns false when the optional contains no value
  56. [endsect]
  57. [section False positive with -Wmaybe-uninitialized]
  58. Sometimes on GCC compilers below version 5.1 you may get an -Wmaybe-uninitialized warning when copiling with option -02 on a perfectly valid `boost::optional` usage. For instance in this program:
  59. #include <boost/optional.hpp>
  60. boost::optional<int> getitem();
  61. int main(int argc, const char *[])
  62. {
  63. boost::optional<int> a = getitem();
  64. boost::optional<int> b;
  65. if (argc > 0)
  66. b = argc;
  67. if (a != b)
  68. return 1;
  69. return 0;
  70. }
  71. This is a bug in the compiler. As a workaround (provided in [@http://stackoverflow.com/questions/21755206/how-to-get-around-gcc-void-b-4-may-be-used-uninitialized-in-this-funct this Stack Overflow question]) use the following way of initializing an optional containing no value:
  72. boost::optional<int> b = boost::make_optional(false, int());
  73. This is obviously redundant, but makes the warning disappear.
  74. [endsect]
  75. [endsect]