diff --git a/include/boost/optional/detail/optional_reference_spec.hpp b/include/boost/optional/detail/optional_reference_spec.hpp index ba3951a..4d1552f 100644 --- a/include/boost/optional/detail/optional_reference_spec.hpp +++ b/include/boost/optional/detail/optional_reference_spec.hpp @@ -96,6 +96,7 @@ public: template explicit optional(const optional& rhs) BOOST_NOEXCEPT : ptr_(rhs.ptr_) {} optional(const optional& rhs) BOOST_NOEXCEPT : ptr_(rhs.ptr_) {} + optional(T& rhs) BOOST_NOEXCEPT : ptr_(boost::addressof(rhs)) {} optional& operator=(const optional& rhs) BOOST_NOEXCEPT { ptr_ = rhs.ptr_; return *this; } @@ -121,6 +122,8 @@ public: #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES + optional(T&& rhs) BOOST_NOEXCEPT { detail::prevent_binding_rvalue(); } + template optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if >::type* = 0) BOOST_NOEXCEPT : ptr_(boost::addressof(r)) { detail::prevent_binding_rvalue(); } diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 7394ce8..180bbab 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -20,6 +20,7 @@ import testing ; [ run optional_test.cpp ] [ run optional_test_swap.cpp ] [ run optional_test_conversions_from_U.cpp ] + [ run optional_test_convert_from_T.cpp ] [ run optional_test_tie.cpp ] [ run optional_test_ref_assign_portable_minimum.cpp ] [ run optional_test_ref_assign_mutable_int.cpp ] diff --git a/test/optional_test_convert_from_T.cpp b/test/optional_test_convert_from_T.cpp new file mode 100644 index 0000000..d4ce4bb --- /dev/null +++ b/test/optional_test_convert_from_T.cpp @@ -0,0 +1,44 @@ +// Copyright (C) 2014 Andrzej Krzemienski. +// +// Use, modification, and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org/lib/optional for documentation. +// +// You are welcome to contact the author at: +// akrzemi1@gmail.com + +#include "boost/optional/optional.hpp" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + +#include "boost/core/lightweight_test.hpp" +#include "boost/none.hpp" + +//#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR + +using boost::optional; +using boost::none; + +void test_optional_optional_T() +{ + optional oi1 (1), oiN; + optional< optional > ooi1 (oi1), ooiN(oiN); + + BOOST_TEST(ooi1); + BOOST_TEST(*ooi1); + BOOST_TEST_EQ(**ooi1, 1); + + BOOST_TEST(ooiN); + BOOST_TEST(!*ooiN); +} + +int main() +{ + test_optional_optional_T(); + + return boost::report_errors(); +} diff --git a/test/optional_test_ref_assign_portable_minimum.cpp b/test/optional_test_ref_assign_portable_minimum.cpp index 9ee0a0f..19085d3 100644 --- a/test/optional_test_ref_assign_portable_minimum.cpp +++ b/test/optional_test_ref_assign_portable_minimum.cpp @@ -39,6 +39,7 @@ int main() { test_optional_ref_assignment(); test_optional_ref_assignment(); + test_optional_ref_assignment< optional >(); return boost::report_errors(); } diff --git a/test/optional_test_ref_converting_ctor.cpp b/test/optional_test_ref_converting_ctor.cpp index 735c354..81f3c0f 100644 --- a/test/optional_test_ref_converting_ctor.cpp +++ b/test/optional_test_ref_converting_ctor.cpp @@ -102,6 +102,7 @@ int main() test_all_const_cases(); test_all_const_cases(); test_all_const_cases(); + test_all_const_cases< optional >(); return boost::report_errors(); } diff --git a/test/optional_test_ref_portable_minimum.cpp b/test/optional_test_ref_portable_minimum.cpp index 5bd7d11..303559a 100644 --- a/test/optional_test_ref_portable_minimum.cpp +++ b/test/optional_test_ref_portable_minimum.cpp @@ -465,10 +465,12 @@ int main() test_optional_ref(); test_optional_ref(); test_optional_ref(); + test_optional_ref< optional >(); test_optional_const_ref(); test_optional_const_ref(); test_optional_const_ref(); + test_optional_const_ref< optional >(); return boost::report_errors(); } diff --git a/test/testable_classes.hpp b/test/testable_classes.hpp index e18359f..3b2a436 100644 --- a/test/testable_classes.hpp +++ b/test/testable_classes.hpp @@ -12,6 +12,8 @@ #ifndef BOOST_OPTIONAL_TEST_TESTABKE_CLASSES_AK_07JAN2015_HPP #define BOOST_OPTIONAL_TEST_TESTABKE_CLASSES_AK_07JAN2015_HPP +#include "boost/optional/optional.hpp" + struct ScopeGuard // no copy/move ctor/assign { int val_; @@ -74,13 +76,23 @@ struct has_arrow static const bool value = false; }; +template <> +struct has_arrow< boost::optional > +{ + static const bool value = false; +}; + int& val(int& i) { return i; } int& val(Abstract& a) { return a.val(); } +int& val(Impl& a) { return a.val(); } int& val(ScopeGuard& g) { return g.val(); } +template int& val(T& o) { return *o; } const int& val(const int& i) { return i; } const int& val(const Abstract& a) { return a.val(); } +const int& val(const Impl& a) { return a.val(); } const int& val(const ScopeGuard& g) { return g.val(); } +template const int& val(const T& o) { return *o; } bool operator==(const Abstract& l, const Abstract& r) { return l.val() == r.val(); } bool operator==(const ScopeGuard& l, const ScopeGuard& r) { return l.val() == r.val(); }