From 734e5b52835a79a820e88e96c69c4d9bcb1d5def Mon Sep 17 00:00:00 2001
From: Fernando Cacciola Effect: Constructs an optional uninitialized. Effect: Constructs an optional uninitialized. Postconditions: *this is uninitialized. Throws: Nothing. Notes: Effect: Directly-Constructs an optional. Postconditions: *this is initialized and its value is a copy of 'v'. Throws: Whatever T::T( T const& ) throws. Notes: T::T( T const& ) is called. If condition is otherwise, same as: Effect: Copy-Constructs an optional. Returns: A reference to the contained value, if any, or Throws: Nothing. Example: Returns: optional<T>(v) for the deduced type Example: Returns: optional<T>(condition,v) for the deduced type Example: Returns: If both x and y are initialied, optional<T>::optional( none_t );
-
optional<T (not a ref)>::optional( T const& v )
+optional<T (not a ref)>::optional( bool condition, T const& v ) ;
+optional<T&> ::optional( bool condition, T& v ) ;
+
+
+
+
+
+true
, same as:optional<T (not a ref)>::optional( T const& v )
+optional<T&> ::optional( T& v )
+
+optional<T (not a ref)>::optional()
+optional<T&> ::optional()
+
+
+
optional<T (not a ref)>::optional( optional const& rhs );
-
T const& optional<T (not a ref)>::operator*() const ;
T& optional<T (not a ref)>::operator*();
@@ -828,6 +852,32 @@ assert ( *opt == w ) ;
+T const& optional<T (not a ref)>::get_value_or( T const& default) const ;
+T& optional<T (not a ref)>::get_value_or( T& default ) ;
+
+inline T const& get_optional_value_or ( optional<T (not a ref)> const& o, T const& default ) ;
+inline T& get_optional_value_or ( optional<T (not a ref)>& o, T& default ) ;
+
+
+
+default
+
+
+T v, z ;
+optional<T> def;
+T const& y = def.get_value_or(z);
+assert ( y == z ) ;
+
+optional<T> opt ( v );
+T const& u = get_optional_value_or(opt,z);
+assert ( u == v ) ;
+assert ( u != z ) ;
+
+
+
T const& optional<T&>::operator*() const ;
T & optional<T&>::operator*();
@@ -965,18 +1015,47 @@ assert ( opt.is_initialized() );
+optional<T (not a ref)> make_optional( T const& v )
+
+
+T
of v
.
+
+template<class T> void foo ( optional<T> const& opt ) ;
+
+foo ( make_optional(1+1) ) ; // Creates an optional<int>
+
+
+optional<T (not a ref)> make_optional( bool condition, T const& v )
+
+
+
+T
of v
.
+
+optional<double> calculate_foo()
+{
+ double val = compute_foo();
+ return make_optional(is_not_nan_and_finite(val),val);
+}
+
+optional<double> v = calculate_foo();
+if ( !v )
+ error("foo wasn't computed");
+
+
bool operator == ( optional<T> const& x, optional<T> const& y );
(*x == *y)
.
-If only x or y is initialized, false
. If both are uninitialized, true
.
-false
. If both are uninitialized, true
.
Throws: Nothing.
Notes: Pointers have shallow relational operators while optional has deep relational operators. Do not use operator == directly in generic code which expect to be given either an optional<T> or a pointer; -use equal_pointees() instead -
+use equal_pointees() insteadExample:
T x(12); @@ -1012,14 +1091,12 @@ assert ( optX != optZ ) ;Returns: If y is not initialized,
+If both x and y are initialized,false
. If y is initialized and x is not initialized,true
. -If both x and y are initialized,(*x < *y)
. -(*x < *y)
.Throws: Nothing.
Notes: Pointers have shallow relational operators while optional has deep relational operators. Do not use operator < directly in generic code which expect to be given either an optional<T> or a pointer; -use less_pointees() instead -
+use less_pointees() insteadExample:
T x(12); @@ -1082,23 +1159,17 @@ assert ( optX != optZ ) ;-void swap ( optional<T>& x, optional<T>& y );-Effect: If both x and y are initialized, calls
swap(*x,*y)
-using std::swap.
+Effect: If both x and y are initialized, calls
+If none is initialized, does nothing.swap(*x,*y)
using std::swap.
If only one is initialized, say x, calls:y.reset(*x); x.reset();
-If none is initialized, does nothing. -Postconditions: The states of x and y interchanged.
Throws: If both are initialized, whatever swap(T&,T&) throws. -If only one is initialized, whatever T::T ( T const& ) throws. -
-Notes: If both are initialized, swap(T&,T&) is used unqualified -but with std::swap introduced in scope.
+If only one is initialized, whatever T::T ( T const& ) throws. +
-If only one is initialized, T::~T() and T::T( T const& ) is called. -Notes: If both are initialized, swap(T&,T&) is used unqualified but with std::swap introduced in scope.
+If only one is initialized, T::~T() and T::T( T const& ) is called.Exception Safety: If both are initialized, this operation has the exception safety guarantees of swap(T&,T&).
+If only one is initialized, it has the same basic guarantee as optional<T>::reset( T const& ).
-If only one is initialized, it has the same basic guarantee as optional<T>::reset( T const& ). -Example:
T x(12); @@ -1254,8 +1325,7 @@ assert(a==b); b = 3 ; assert(ra!=b); // 'ra' is not rebound to 'b'-Now, if you assign to an initialized optional<T&>, the effect is to -rebind to the new object instead of assigning the referee. This is unlike +
Now, if you assign to an initialized optional<T&>, the effect is to rebind to the new object instead of assigning the referee. This is unlike bare C++ references.
int a = 1 ; int b = 2 ; @@ -1358,8 +1428,7 @@ public:A limitation of this method is that it doesn't scale well to wrapped objects with multiple constructors nor to generic code were the constructor overloads are unknown.
-The solution presented in this library is the family of InPlaceFactories and -TypedInPlaceFactories.
+The solution presented in this library is the family of InPlaceFactories and TypedInPlaceFactories.
@@ -1427,10 +1496,7 @@ public: W ( in_place(123,"hello") ) ; }
These factories are a family of classes which encapsulate an increasing number of arbitrary constructor parameters and supply a method to construct an object of a given type using those parameters at an address specified by the user via placement new.The factories are implemented in the headers: -in_place_factory.hpp and -typed_in_place_factory.hpp -
+The factories are implemented in the headers: in_place_factory.hpp and typed_in_place_factory.hpp
@@ -1473,8 +1539,7 @@ of the assignment methods: TypedInPlaceFactory const& )- optional<T>:::reset ( T const&)
Can only guarantee the basic exception safety: The lvalue optional is left uninitialized -if an exception is thrown (any previous value is first destroyed using T::~T())
+Can only guarantee the basic exception safety: The lvalue optional is left uninitialized if an exception is thrown (any previous value is first destroyed using T::~T())
On the other hand, the uninitializing methods:
- @@ -1603,12 +1668,9 @@ T is not required to be -LICENSE_1_0.txt or copy at -www.boost.org/LICENSE_1_0.txt) +License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt)
optional<T>::operator= ( detail::none_t )
Developed by Fernando Cacciola, the latest version of this file can be found at www.boost.org, and the boost -discussion lists
- +HREF="http://www.boost.org">www.boost.org, and the boost discussion lists +