📄 optional.hpp
字号:
pointer_type cast_ptr( internal_type * p, is_not_reference_tag ) { return p ; } bool m_initialized ; storage_type m_storage ;} ;} // namespace optional_detailtemplate<class T>class optional : public optional_detail::optional_base<T>{ typedef optional_detail::optional_base<T> base ; typedef BOOST_DEDUCED_TYPENAME base::unspecified_bool_type unspecified_bool_type ; public : typedef optional<T> this_type ; typedef BOOST_DEDUCED_TYPENAME base::value_type value_type ; typedef BOOST_DEDUCED_TYPENAME base::reference_type reference_type ; typedef BOOST_DEDUCED_TYPENAME base::reference_const_type reference_const_type ; typedef BOOST_DEDUCED_TYPENAME base::pointer_type pointer_type ; typedef BOOST_DEDUCED_TYPENAME base::pointer_const_type pointer_const_type ; typedef BOOST_DEDUCED_TYPENAME base::argument_type argument_type ; // Creates an optional<T> uninitialized. // No-throw optional() : base() {} // Creates an optional<T> uninitialized. // No-throw optional( detail::none_t const& none_ ) : base(none_) {} // Creates an optional<T> initialized with 'val'. // Can throw if T::T(T const&) does optional ( argument_type val ) : base(val) {}#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR // NOTE: MSVC needs templated versions first // Creates a deep copy of another convertible optional<U> // Requires a valid conversion from U to T. // Can throw if T::T(U const&) does template<class U> explicit optional ( optional<U> const& rhs ) : base() { if ( rhs.is_initialized() ) this->construct(rhs.get()); }#endif#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT // Creates an optional<T> with an expression which can be either // (a) An instance of InPlaceFactory (i.e. in_place(a,b,...,n); // (b) An instance of TypedInPlaceFactory ( i.e. in_place<T>(a,b,...,n); // (c) Any expression implicitely convertible to the single type // of a one-argument T's constructor. // (d*) Weak compilers (BCB) might also resolved Expr as optional<T> and optional<U> // even though explicit overloads are present for these. // Depending on the above some T ctor is called. // Can throw is the resolved T ctor throws. template<class Expr> explicit optional ( Expr const& expr ) : base(expr,&expr) {}#endif // Creates a deep copy of another optional<T> // Can throw if T::T(T const&) does optional ( optional const& rhs ) : base(rhs) {} // No-throw (assuming T::~T() doesn't) ~optional() {}#if !defined(BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT) && !defined(BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION) // Assigns from an expression. See corresponding constructor. // Basic Guarantee: If the resolved T ctor throws, this is left UNINITIALIZED template<class Expr> optional& operator= ( Expr expr ) { this->assign_expr(expr,&expr); return *this ; }#endif#ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT // Assigns from another convertible optional<U> (converts && deep-copies the rhs value) // Requires a valid conversion from U to T. // Basic Guarantee: If T::T( U const& ) throws, this is left UNINITIALIZED template<class U> optional& operator= ( optional<U> const& rhs ) { this->destroy(); // no-throw if ( rhs.is_initialized() ) { // An exception can be thrown here. // It it happens, THIS will be left uninitialized. this->assign(rhs.get()); } return *this ; }#endif // Assigns from another optional<T> (deep-copies the rhs value) // Basic Guarantee: If T::T( T const& ) throws, this is left UNINITIALIZED // (NOTE: On BCB, this operator is not actually called and left is left UNMODIFIED in case of a throw) optional& operator= ( optional const& rhs ) { this->assign( rhs ) ; return *this ; } // Assigns from a T (deep-copies the rhs value) // Basic Guarantee: If T::( T const& ) throws, this is left UNINITIALIZED optional& operator= ( argument_type val ) { this->assign( val ) ; return *this ; } // Assigns from a "none" // Which destroys the current value, if any, leaving this UNINITIALIZED // No-throw (assuming T::~T() doesn't) optional& operator= ( detail::none_t const& none_ ) { this->assign( none_ ) ; return *this ; } // Returns a reference to the value if this is initialized, otherwise, // the behaviour is UNDEFINED // No-throw reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); } reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); } // Returns a pointer to the value if this is initialized, otherwise, // the behaviour is UNDEFINED // No-throw pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; } pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; } // Returns a reference to the value if this is initialized, otherwise, // the behaviour is UNDEFINED // No-throw reference_const_type operator *() const { return this->get() ; } reference_type operator *() { return this->get() ; } // implicit conversion to "bool" // No-throw operator unspecified_bool_type() const { return this->safe_bool() ; } // This is provided for those compilers which don't like the conversion to bool // on some contexts. bool operator!() const { return !this->is_initialized() ; }} ;// Returns a reference to the value if this is initialized, otherwise, the behaviour is UNDEFINED.// No-throwtemplate<class T>inlineBOOST_DEDUCED_TYPENAME optional<T>::reference_const_typeget ( optional<T> const& opt ){ return opt.get() ;}template<class T>inlineBOOST_DEDUCED_TYPENAME optional<T>::reference_typeget ( optional<T>& opt ){ return opt.get() ;}// Returns a pointer to the value if this is initialized, otherwise, returns NULL.// No-throwtemplate<class T>inlineBOOST_DEDUCED_TYPENAME optional<T>::pointer_const_typeget ( optional<T> const* opt ){ return opt->get_ptr() ;}template<class T>inlineBOOST_DEDUCED_TYPENAME optional<T>::pointer_typeget ( optional<T>* opt ){ return opt->get_ptr() ;}// Returns a pointer to the value if this is initialized, otherwise, returns NULL.// No-throwtemplate<class T>inlineBOOST_DEDUCED_TYPENAME optional<T>::pointer_const_typeget_pointer ( optional<T> const& opt ){ return opt.get_ptr() ;}template<class T>inlineBOOST_DEDUCED_TYPENAME optional<T>::pointer_typeget_pointer ( optional<T>& opt ){ return opt.get_ptr() ;}// optional's relational operators ( ==, !=, <, >, <=, >= ) have deep-semantics (compare values).// WARNING: This is UNLIKE pointers. Use equal_pointees()/less_pointess() in generic code instead.template<class T>inlinebool operator == ( optional<T> const& x, optional<T> const& y ){ return equal_pointees(x,y); }template<class T>inlinebool operator < ( optional<T> const& x, optional<T> const& y ){ return less_pointees(x,y); }template<class T>inlinebool operator != ( optional<T> const& x, optional<T> const& y ){ return !( x == y ) ; }template<class T>inlinebool operator > ( optional<T> const& x, optional<T> const& y ){ return y < x ; }template<class T>inlinebool operator <= ( optional<T> const& x, optional<T> const& y ){ return !( y < x ) ; }template<class T>inlinebool operator >= ( optional<T> const& x, optional<T> const& y ){ return !( x < y ) ; }template<class T>inlinebool operator == ( optional<T> const& x, detail::none_t const& ){ return equal_pointees(x, optional<T>() ); }template<class T>inlinebool operator < ( optional<T> const& x, detail::none_t const& ){ return less_pointees(x,optional<T>() ); }template<class T>inlinebool operator != ( optional<T> const& x, detail::none_t const& y ){ return !( x == y ) ; }template<class T>inlinebool operator > ( optional<T> const& x, detail::none_t const& y ){ return y < x ; }template<class T>inlinebool operator <= ( optional<T> const& x, detail::none_t const& y ){ return !( y < x ) ; }template<class T>inlinebool operator >= ( optional<T> const& x, detail::none_t const& y ){ return !( x < y ) ; }template<class T>inlinebool operator == ( detail::none_t const& x, optional<T> const& y ){ return equal_pointees(optional<T>() ,y); }template<class T>inlinebool operator < ( detail::none_t const& x, optional<T> const& y ){ return less_pointees(optional<T>() ,y); }template<class T>inlinebool operator != ( detail::none_t const& x, optional<T> const& y ){ return !( x == y ) ; }template<class T>inlinebool operator > ( detail::none_t const& x, optional<T> const& y ){ return y < x ; }template<class T>inlinebool operator <= ( detail::none_t const& x, optional<T> const& y ){ return !( y < x ) ; }template<class T>inlinebool operator >= ( detail::none_t const& x, optional<T> const& y ){ return !( x < y ) ; }//// The following swap implementation follows the GCC workaround as found in// "boost/detail/compressed_pair.hpp"//namespace optional_detail {// GCC < 3.2 gets the using declaration at namespace scope (FLC, DWA)#if BOOST_WORKAROUND(__GNUC__, < 3) \ || BOOST_WORKAROUND(__GNUC__, == 3) && __GNUC_MINOR__ <= 2 using std::swap;#define BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE#endif// optional's swap:// If both are initialized, calls swap(T&, T&), with whatever exception guarantess are given there.// If only one is initialized, calls I.reset() and U.reset(*I), with the Basic Guarantee// If both are uninitialized, do nothing (no-throw)template<class T>inlinevoid optional_swap ( optional<T>& x, optional<T>& y ){ if ( !x && !!y ) { x.reset(*y); // Basic guarantee. y.reset(); } else if ( !!x && !y ) { y.reset(*x); // Basic guarantee. x.reset(); } else if ( !!x && !!y ) {// GCC > 3.2 and all other compilers have the using declaration at function scope (FLC)#ifndef BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE // allow for Koenig lookup using std::swap ;#endif swap(*x,*y); }}} // namespace optional_detailtemplate<class T> inline void swap ( optional<T>& x, optional<T>& y ){ optional_detail::optional_swap(x,y);}} // namespace boost#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -