19_type_requirements.qbk 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 
  2. [section Type requirements]
  3. The very minimum requirement of `optional<T>` is that `T` is a complete type and that it has a publicly accessible destructor. `T` doesn't even need to be constructible. You can use a very minimum interface:
  4. optional<T> o; // uninitialized
  5. assert(o == none); // check if initialized
  6. assert(!o); //
  7. o.value(); // always throws
  8. But this is practically useless. In order for `optional<T>` to be able to do anything useful and offer all the spectrum of ways of accessing the contained value, `T` needs to have at least one accessible constructor. In that case you need to initialize the optional object with function `emplace()`, or if your compiler does not support it, resort to [link boost_optional.tutorial.in_place_factories In-Place Factories]:
  9. optional<T> o;
  10. o.emplace("T", "ctor", "params");
  11. If `T` is __MOVE_CONSTRUCTIBLE__, `optional<T>` is also __MOVE_CONSTRUCTIBLE__ and can be easily initialized from an rvalue of type `T` and be passed by value:
  12. optional<T> o = make_T();
  13. optional<T> p = optional<T>();
  14. If `T` is __COPY_CONSTRUCTIBLE__, `optional<T>` is also __COPY_CONSTRUCTIBLE__ and can be easily initialized from an lvalue of type `T`:
  15. T v = make_T();
  16. optional<T> o = v;
  17. optional<T> p = o;
  18. If `T` is not `MoveAssignable`, it is still possible to reset the value of `optional<T>` using function `emplace()`:
  19. optional<const T> o = make_T();
  20. o.emplace(make_another_T());
  21. If `T` is `Moveable` (both __MOVE_CONSTRUCTIBLE__ and `MoveAssignable`) then `optional<T>` is also `Moveable` and additionally can be constructed and assigned from an rvalue of type `T`.
  22. Similarly, if `T` is `Copyable` (both __COPY_CONSTRUCTIBLE__ and `CopyAssignable`) then `optional<T>` is also `Copyable` and additionally can be constructed and assigned from an lvalue of type `T`.
  23. `T` ['is not] required to be __SGI_DEFAULT_CONSTRUCTIBLE__.
  24. [endsect]