Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Fix bug with expected::value(). #40

Merged
merged 3 commits into from
Nov 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions 3rd_party/include/opentracing/expected/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ union storage_t<void, E>

/// class unexpected_type

template< typename E = std::exception_ptr >
template< typename E = std::error_code >
class unexpected_type
{
public:
Expand Down Expand Up @@ -384,6 +384,35 @@ class bad_expected_access : public std::logic_error
error_type m_error;
};

/// class error_traits

template< typename Error >
struct error_traits
{
static void rethrow( Error const & e )
{
throw bad_expected_access<Error>{ e };
}
};

template<>
struct error_traits< std::exception_ptr >
{
static void rethrow( std::exception_ptr const & e )
{
std::rethrow_exception( e );
}
};

template<>
struct error_traits< std::error_code >
{
static void rethrow( std::error_code const & e )
{
throw std::system_error( e );
}
};

/// class expected

template< typename T, typename E = std::error_code >
Expand Down Expand Up @@ -652,28 +681,22 @@ class expected
constexpr value_type const & value() const &
{
return has_value()
? contained.value()
: std::is_same<error_type, std::exception_ptr>::value
? ( std::rethrow_exception( contained.error() ), contained.value() )
: ( throw bad_expected_access<error_type>( contained.error() ), contained.value() );
? ( contained.value() )
: ( error_traits<error_type>::rethrow( contained.error() ), contained.value() );
}

value_type & value() &
{
return has_value()
? contained.value()
: std::is_same<error_type, std::exception_ptr>::value
? ( std::rethrow_exception( contained.error() ), contained.value() )
: ( throw bad_expected_access<error_type>( contained.error() ), contained.value() );
? ( contained.value() )
: ( error_traits<error_type>::rethrow( contained.error() ), contained.value() );
}

constexpr value_type && value() const &&
constexpr value_type && value() &&
{
return has_value()
? std::move( contained.value() )
: std::is_same<error_type, std::exception_ptr>::value
? ( std::rethrow_exception( contained.error() ), contained.value() )
: ( throw bad_expected_access<error_type>( contained.error() ), contained.value() );
? ( contained.value() )
: ( error_traits<error_type>::rethrow( contained.error() ), contained.value() );
}

constexpr error_type const & error() const &
Expand Down