diff options
Diffstat (limited to 'boost/safe_numerics/checked_result.hpp')
-rw-r--r-- | boost/safe_numerics/checked_result.hpp | 73 |
1 files changed, 61 insertions, 12 deletions
diff --git a/boost/safe_numerics/checked_result.hpp b/boost/safe_numerics/checked_result.hpp index 55efca082a..10bd7a65c8 100644 --- a/boost/safe_numerics/checked_result.hpp +++ b/boost/safe_numerics/checked_result.hpp @@ -1,11 +1,6 @@ #ifndef BOOST_NUMERIC_CHECKED_RESULT #define BOOST_NUMERIC_CHECKED_RESULT -// MS compatible compilers support #pragma once -#if defined(_MSC_VER) && (_MSC_VER >= 1020) -# pragma once -#endif - // Copyright (c) 2012 Robert Ramey // // Distributed under the Boost Software License, Version 1.0. (See @@ -32,21 +27,54 @@ struct checked_result { // don't permit construction without initial value; checked_result() = delete; + // note: I implemented the following non-default copy and move + // constructors because I thought I needed to do this in order + // to make them constexpr. Turns out though that doing this creates + // a syntax error because the assignment results in error due + // to assignment "outside of object lifetime". I think this could + // be addressed by replacing the anonymous union above with a + // named union. This would create some syntax changes which would + // ripple through some parts of th program. So for now, we'll just + // rely on the default copy and move constructors. + #if 0 + // copy constructor + constexpr /*explicit*/ checked_result(const checked_result & r) noexpect : + m_e(r.m_e) + { + if(safe_numerics_error::success == r.m_e) + m_r = r.m_r; + else + m_msg = r.m_msg; + } + + // move constructor + constexpr /*explicit*/ checked_result(checked_result && r) noexcept : + m_e(r.m_e) + { + if(safe_numerics_error::success == r.m_e) + m_r = r.m_r; + else + m_msg = r.m_msg; + } + #endif + checked_result(const checked_result & r) = default; + checked_result(checked_result && r) = default; + constexpr /*explicit*/ checked_result(const R & r) : m_e(safe_numerics_error::success), m_r(r) {} #if 0 template<typename T> - constexpr /*explicit*/ checked_result(const T & t) : + constexpr /*explicit*/ checked_result(const T & t) noexcept : m_e(safe_numerics_error::success), m_r(t) {} #endif constexpr /*explicit*/ checked_result( - safe_numerics_error e, + const safe_numerics_error & e, const char * msg = "" - ) : + ) noexcept : m_e(e), m_msg(msg) { @@ -54,7 +82,7 @@ struct checked_result { } // permit construct from another checked result type template<typename T> - constexpr /*explicit*/ checked_result(const checked_result<T> & t) : + constexpr /*explicit*/ checked_result(const checked_result<T> & t) noexcept : m_e(t.m_e) { static_assert( @@ -71,18 +99,18 @@ struct checked_result { } // accesors - constexpr operator R() const { + constexpr operator R() const noexcept{ // don't assert here. Let the library catch these errors assert(! exception()); return m_r; } - constexpr operator safe_numerics_error () const { + constexpr operator safe_numerics_error () const noexcept{ // note that this is a legitimate operation even when // the operation was successful - it will return success return m_e; } - constexpr operator const char *() const { + constexpr operator const char *() const noexcept{ assert(exception()); return m_msg; } @@ -91,6 +119,27 @@ struct checked_result { checked_result & operator=(const checked_result &) = delete; }; +#if 0 +template<typename R> +constexpr checked_result<R> make_checked_result( + const safe_numerics_error & e, + char const * const & m +) noexcept { + return checked_result<R>(e, m); +} +#endif + +template <class R> +class make_checked_result { +public: + template<safe_numerics_error E> + constexpr static checked_result<R> invoke( + char const * const & m + ) noexcept { + return checked_result<R>(E, m); + } +}; + } // safe_numerics } // boost |