diff --git a/include/boost/system/result.hpp b/include/boost/system/result.hpp index c30e1c7..c492f37 100644 --- a/include/boost/system/result.hpp +++ b/include/boost/system/result.hpp @@ -210,12 +210,31 @@ public: } } - BOOST_CXX14_CONSTEXPR T&& value() && + template + BOOST_CXX14_CONSTEXPR + typename std::enable_if::value, T>::type + value() && { return std::move( value() ); } - BOOST_CXX14_CONSTEXPR T const&& value() const&& + template + BOOST_CXX14_CONSTEXPR + typename std::enable_if::value, T&&>::type + value() && + { + return std::move( value() ); + } + + template + BOOST_CXX14_CONSTEXPR + typename std::enable_if::value, T>::type + value() const && = delete; + + template + BOOST_CXX14_CONSTEXPR + typename std::enable_if::value, T const&&>::type + value() const && { return std::move( value() ); } @@ -274,12 +293,31 @@ public: return *p; } - BOOST_CXX14_CONSTEXPR T&& operator*() && noexcept + template + BOOST_CXX14_CONSTEXPR + typename std::enable_if::value, T>::type + operator*() && noexcept(std::is_nothrow_move_constructible::value) { return std::move(**this); } - BOOST_CXX14_CONSTEXPR T const&& operator*() const && noexcept + template + BOOST_CXX14_CONSTEXPR + typename std::enable_if::value, T&&>::type + operator*() && noexcept + { + return std::move(**this); + } + + template + BOOST_CXX14_CONSTEXPR + typename std::enable_if::value, T>::type + operator*() const && noexcept = delete; + + template + BOOST_CXX14_CONSTEXPR + typename std::enable_if::value, T const&&>::type + operator*() const && noexcept { return std::move(**this); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bab504b..4798718 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -125,3 +125,4 @@ boost_test(TYPE run SOURCES result_value_access.cpp) boost_test(TYPE run SOURCES result_error_access.cpp) boost_test(TYPE run SOURCES result_swap.cpp) boost_test(TYPE run SOURCES result_eq.cpp) +boost_test(TYPE run SOURCES result_range_for.cpp) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 9c1cc4b..f48340b 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -149,3 +149,4 @@ run result_value_access.cpp : : : $(CPP11) ; run result_error_access.cpp : : : $(CPP11) ; run result_swap.cpp : : : $(CPP11) gcc-10:"-Wno-maybe-uninitialized" ; run result_eq.cpp : : : $(CPP11) ; +run result_range_for.cpp : : : $(CPP11) ; diff --git a/test/result_range_for.cpp b/test/result_range_for.cpp new file mode 100644 index 0000000..8407048 --- /dev/null +++ b/test/result_range_for.cpp @@ -0,0 +1,30 @@ +// Copyright 2021 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +using namespace boost::system; + +result> f() +{ + return std::vector{ 1, 2, 3 }; +} + +int main() +{ + { + int s = 0; + + for( int x: f().value() ) + { + s += x; + } + + BOOST_TEST_EQ( s, 6 ); + } + + return boost::report_errors(); +}