1 // Copyright (C) 2015-2016 Andrzej Krzemienski.
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/optional for documentation.
9 // You are welcome to contact the author at:
12 #ifndef BOOST_OPTIONAL_DETAIL_OPTIONAL_REFERENCE_SPEC_AJK_03OCT2015_HPP
13 #define BOOST_OPTIONAL_DETAIL_OPTIONAL_REFERENCE_SPEC_AJK_03OCT2015_HPP
15 #ifdef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT
16 #include <boost/type_traits/is_integral.hpp>
17 #include <boost/type_traits/is_const.hpp>
26 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
29 void prevent_binding_rvalue()
31 #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
32 BOOST_STATIC_ASSERT_MSG(boost::is_lvalue_reference<From>::value,
33 "binding rvalue references to optional lvalue references is disallowed");
38 BOOST_DEDUCED_TYPENAME boost::remove_reference<T>::type& forward_reference(T&& r)
40 BOOST_STATIC_ASSERT_MSG(boost::is_lvalue_reference<T>::value,
41 "binding rvalue references to optional lvalue references is disallowed");
42 return boost::forward<T>(r);
45 #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
49 struct is_const_integral
51 static const bool value = boost::is_const<T>::value && boost::is_integral<T>::value;
55 struct is_const_integral_bad_for_conversion
57 #if (!defined BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES) && (defined BOOST_OPTIONAL_CONFIG_NO_PROPER_CONVERT_FROM_CONST_INT)
58 static const bool value = boost::is_const<T>::value && boost::is_integral<T>::value;
60 static const bool value = false;
65 void prevent_assignment_from_false_const_integral()
67 #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
68 #ifdef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT
69 // MSVC compiler without rvalue refernces: we need to disable the asignment from
70 // const integral lvalue reference, as it may be an invalid temporary
71 BOOST_STATIC_ASSERT_MSG(!is_const_integral<From>::value,
72 "binding const lvalue references to integral types is disabled in this compiler");
81 static const bool value = false;
85 struct is_optional_< ::boost::optional<U> >
87 static const bool value = true;
93 static const bool value = !is_optional_<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>::value;
97 template <class T, class U>
98 struct is_same_decayed
100 static const bool value = ::boost::is_same<T, BOOST_DEDUCED_TYPENAME ::boost::remove_reference<U>::type>::value
101 || ::boost::is_same<T, const BOOST_DEDUCED_TYPENAME ::boost::remove_reference<U>::type>::value;
104 template <class T, class U>
105 struct no_unboxing_cond
107 static const bool value = is_no_optional<U>::value && !is_same_decayed<T, U>::value;
111 } // namespace detail
114 class optional<T&> : public optional_detail::optional_tag
119 typedef T& value_type;
120 typedef T& reference_type;
121 typedef T& reference_const_type;
122 typedef T& rval_reference_type;
123 typedef T* pointer_type;
124 typedef T* pointer_const_type;
126 optional() BOOST_NOEXCEPT : ptr_() {}
127 optional(none_t) BOOST_NOEXCEPT : ptr_() {}
130 explicit optional(const optional<U&>& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {}
131 optional(const optional& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {}
133 // the following two implement a 'conditionally explicit' constructor: condition is a hack for buggy compilers with srewed conversion construction from const int
135 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
136 : ptr_(boost::addressof(rhs)) {}
139 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
140 : ptr_(boost::addressof(rhs)) {}
142 optional& operator=(const optional& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; }
144 optional& operator=(const optional<U&>& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; }
145 optional& operator=(none_t) BOOST_NOEXCEPT { ptr_ = 0; return *this; }
148 void swap(optional& rhs) BOOST_NOEXCEPT { std::swap(ptr_, rhs.ptr_); }
149 T& get() const { BOOST_ASSERT(ptr_); return *ptr_; }
151 T* get_ptr() const BOOST_NOEXCEPT { return ptr_; }
152 T* operator->() const { BOOST_ASSERT(ptr_); return ptr_; }
153 T& operator*() const { BOOST_ASSERT(ptr_); return *ptr_; }
154 T& value() const { return ptr_ ? *ptr_ : (throw_exception(bad_optional_access()), *ptr_); }
156 bool operator!() const BOOST_NOEXCEPT { return ptr_ == 0; }
157 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
159 void reset() BOOST_NOEXCEPT { ptr_ = 0; }
161 bool is_initialized() const BOOST_NOEXCEPT { return ptr_ != 0; }
163 #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
165 optional(T&& /* rhs */) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<T&&>(); }
168 optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::no_unboxing_cond<T, R> >::type* = 0) BOOST_NOEXCEPT
169 : ptr_(boost::addressof(r)) { detail::prevent_binding_rvalue<R>(); }
172 optional(bool cond, R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R> >::type* = 0) BOOST_NOEXCEPT
173 : ptr_(cond ? boost::addressof(r) : 0) { detail::prevent_binding_rvalue<R>(); }
176 BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, optional<T&>&>::type
177 operator=(R&& r) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); return *this; }
180 void emplace(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R> >::type* = 0) BOOST_NOEXCEPT
181 { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
184 T& get_value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R> >::type* = 0) const BOOST_NOEXCEPT
185 { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
188 T& value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R> >::type* = 0) const BOOST_NOEXCEPT
189 { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
192 void reset(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R> >::type* = 0) BOOST_NOEXCEPT
193 { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
196 T& value_or_eval(F f) const { return ptr_ ? *ptr_ : detail::forward_reference(f()); }
198 #else // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
201 // the following two implement a 'conditionally explicit' constructor
203 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
204 : ptr_(boost::addressof(v)) { }
207 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
208 : ptr_(boost::addressof(v)) { }
211 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) {}
214 BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, optional<T&>&>::type
215 operator=(U& v) BOOST_NOEXCEPT
217 detail::prevent_assignment_from_false_const_integral<U>();
218 ptr_ = boost::addressof(v); return *this;
222 void emplace(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) BOOST_NOEXCEPT
223 { ptr_ = boost::addressof(v); }
226 T& get_value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) const BOOST_NOEXCEPT
227 { return ptr_ ? *ptr_ : v; }
230 T& value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) const BOOST_NOEXCEPT
231 { return ptr_ ? *ptr_ : v; }
234 void reset(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U> >::type* = 0) BOOST_NOEXCEPT
235 { ptr_ = boost::addressof(v); }
238 T& value_or_eval(F f) const { return ptr_ ? *ptr_ : f(); }
240 #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
244 void swap ( optional<T&>& x, optional<T&>& y) BOOST_NOEXCEPT
253 #endif // header guard