Skip to content

Commit

Permalink
Update in-place make_unexpected, add it for use with std::expected, a…
Browse files Browse the repository at this point in the history
…dd test
  • Loading branch information
martinmoene committed Jun 22, 2024
1 parent 5b5d591 commit 91343b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
19 changes: 17 additions & 2 deletions include/nonstd/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@ namespace nonstd {
{
return unexpected< typename std::decay<E>::type >( std::forward<E>(value) );
}

template
<
typename E, typename... Args,
typename = std::enable_if<
std::is_constructible<E, Args...>::value
>
>
constexpr auto
make_unexpected( std::in_place_t inplace, Args &&... args ) -> unexpected_type< typename std::decay<E>::type >
{
return unexpected_type< typename std::decay<E>::type >( inplace, std::forward<Args>(args)...);
}
} // namespace nonstd

#else // nsel_USES_STD_EXPECTED
Expand Down Expand Up @@ -1575,9 +1588,11 @@ template
>
>
nsel_constexpr14 auto
make_unexpected( in_place_t inplace, Args... args ) -> unexpected_type< typename std::decay<E> >
make_unexpected( nonstd_lite_in_place_t(E), Args &&... args ) -> unexpected_type< typename std::decay<E>::type >
{
return unexpected_type< typename std::decay<E>::type >( inplace, std::forward<Args>(args)...);
return std::forward< unexpected_type< typename std::decay<E>::type > >(
unexpected_type< typename std::decay<E>::type >( nonstd_lite_in_place(E), std::forward<Args>(args)...)
);
}

#if nsel_P0323R <= 3
Expand Down
11 changes: 11 additions & 0 deletions test/expected.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,17 @@ CASE( "make_unexpected(): Allows to create an unexpected_type<E> from an E" )
EXPECT( u.error() == error );
}

CASE( "make_unexpected(): Allows to in-place create an unexpected_type<E> from an E" )
{
const auto a = 'a';
const auto b = 7;

auto u = make_unexpected< std::pair<char, int> >( in_place, a, b );

EXPECT( u.error().first == a );
EXPECT( u.error().second == b );
}

CASE( "make_unexpected_from_current_exception(): Allows to create an unexpected_type<std::exception_ptr> from the current exception" "[.deprecated]" )
{
#if nsel_P0323R <= 2
Expand Down

0 comments on commit 91343b0

Please sign in to comment.