diff options
Diffstat (limited to 'boost/optional/detail/optional_reference_spec.hpp')
-rw-r--r-- | boost/optional/detail/optional_reference_spec.hpp | 78 |
1 files changed, 64 insertions, 14 deletions
diff --git a/boost/optional/detail/optional_reference_spec.hpp b/boost/optional/detail/optional_reference_spec.hpp index ba3951af7b..ce558755a2 100644 --- a/boost/optional/detail/optional_reference_spec.hpp +++ b/boost/optional/detail/optional_reference_spec.hpp @@ -44,6 +44,23 @@ BOOST_DEDUCED_TYPENAME boost::remove_reference<T>::type& forward_reference(T&& r #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES + +template <class T> +struct is_const_integral +{ + static const bool value = boost::is_const<T>::value && boost::is_integral<T>::value; +}; + +template <class T> +struct is_const_integral_bad_for_conversion +{ +#if (!defined BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES) && (defined BOOST_OPTIONAL_CONFIG_NO_PROPER_CONVERT_FROM_CONST_INT) + static const bool value = boost::is_const<T>::value && boost::is_integral<T>::value; +#else + static const bool value = false; +#endif +}; + template <class From> void prevent_assignment_from_false_const_integral() { @@ -51,12 +68,13 @@ void prevent_assignment_from_false_const_integral() #ifdef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT // MSVC compiler without rvalue refernces: we need to disable the asignment from // const integral lvalue reference, as it may be an invalid temporary - BOOST_STATIC_ASSERT_MSG(!(boost::is_const<From>::value && boost::is_integral<From>::value), + BOOST_STATIC_ASSERT_MSG(!is_const_integral<From>::value, "binding const lvalue references to integral types is disabled in this compiler"); #endif #endif } + template <class T> struct is_optional_ { @@ -75,6 +93,21 @@ struct is_no_optional static const bool value = !is_optional_<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>::value; }; + +template <class T, class U> + struct is_same_decayed + { + static const bool value = ::boost::is_same<T, BOOST_DEDUCED_TYPENAME ::boost::remove_reference<U>::type>::value + || ::boost::is_same<T, const BOOST_DEDUCED_TYPENAME ::boost::remove_reference<U>::type>::value; + }; + +template <class T, class U> +struct no_unboxing_cond +{ + static const bool value = is_no_optional<U>::value && !is_same_decayed<T, U>::value; +}; + + } // namespace detail template <class T> @@ -94,13 +127,21 @@ public: optional(none_t) BOOST_NOEXCEPT : ptr_() {} template <class U> - explicit optional(const optional<U&>& rhs) BOOST_NOEXCEPT : ptr_(rhs.ptr_) {} - optional(const optional& rhs) BOOST_NOEXCEPT : ptr_(rhs.ptr_) {} + explicit optional(const optional<U&>& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {} + optional(const optional& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {} + // the following two implement a 'conditionally explicit' constructor: condition is a hack for buggy compilers with srewed conversion construction from const int + template <class U> + explicit optional(U& rhs, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::is_same_decayed<T, U>::value && detail::is_const_integral_bad_for_conversion<U>::value>::type* = 0) BOOST_NOEXCEPT + : ptr_(boost::addressof(rhs)) {} + + template <class U> + optional(U& rhs, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::is_same_decayed<T, U>::value && !detail::is_const_integral_bad_for_conversion<U>::value>::type* = 0) BOOST_NOEXCEPT + : ptr_(boost::addressof(rhs)) {} - optional& operator=(const optional& rhs) BOOST_NOEXCEPT { ptr_ = rhs.ptr_; return *this; } + optional& operator=(const optional& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; } template <class U> - optional& operator=(const optional<U&>& rhs) BOOST_NOEXCEPT { ptr_ = rhs.ptr_; return *this; } + optional& operator=(const optional<U&>& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; } optional& operator=(none_t) BOOST_NOEXCEPT { ptr_ = 0; return *this; } @@ -121,8 +162,10 @@ public: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES + optional(T&& /* rhs */) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<T&&>(); } + template <class R> - optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R> >::type* = 0) BOOST_NOEXCEPT + optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::no_unboxing_cond<T, R> >::type* = 0) BOOST_NOEXCEPT : ptr_(boost::addressof(r)) { detail::prevent_binding_rvalue<R>(); } template <class R> @@ -154,19 +197,26 @@ public: #else // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES + + // the following two implement a 'conditionally explicit' constructor + template <class U> + explicit optional(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::no_unboxing_cond<T, U>::value && detail::is_const_integral_bad_for_conversion<U>::value >::type* = 0) BOOST_NOEXCEPT + : ptr_(boost::addressof(v)) { } + template <class U> - optional(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) BOOST_NOEXCEPT : ptr_(boost::addressof(v)) { } + optional(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::no_unboxing_cond<T, U>::value && !detail::is_const_integral_bad_for_conversion<U>::value >::type* = 0) BOOST_NOEXCEPT + : ptr_(boost::addressof(v)) { } template <class U> - optional(bool cond, U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) BOOST_NOEXCEPT : ptr_(cond ? boost::addressof(v) : 0) {} + optional(bool cond, U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) BOOST_NOEXCEPT : ptr_(cond ? boost::addressof(v) : 0) {} template <class U> - BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, optional<T&>&>::type - operator=(U& v) BOOST_NOEXCEPT - { - detail::prevent_assignment_from_false_const_integral<U>(); - ptr_ = boost::addressof(v); return *this; - } + BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, optional<T&>&>::type + operator=(U& v) BOOST_NOEXCEPT + { + detail::prevent_assignment_from_false_const_integral<U>(); + ptr_ = boost::addressof(v); return *this; + } template <class U> void emplace(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) BOOST_NOEXCEPT |