Result
cpp::bitwizeshift::result< void, E > Class Template Reference

Partial specialization of result<void, E> More...

#include <result.hpp>

Public Types

using value_type = void
 The value type of this result.
 
using error_type = E
 The error type of this result.
 
using failure_type = failure< E >
 The failure type.
 
template<typename U >
using rebind = result< U, E >
 Rebinds the result type.
 

Public Member Functions

constexpr result () noexcept
 Constructs a result object in a value state. More...
 
constexpr result (const result &other)=default
 Copy constructs this result. More...
 
constexpr result (result &&other)=default
 Move constructs a result. More...
 
template<typename U , typename E2 , typename = typename std::enable_if<std::is_constructible<E,E2>::value>::type>
 result (const result< U, E2 > &other) noexcept(std::is_nothrow_constructible< E, const E2 &>::value)
 Converting copy constructor. More...
 
template<typename U , typename E2 , typename = typename std::enable_if<std::is_constructible<E,E2>::value>::type>
 result (result< U, E2 > &&other) noexcept(std::is_nothrow_constructible< E, E2 &&>::value)
 Converting move constructor. More...
 
constexpr result (in_place_t) noexcept
 Constructs a result object in a value state. More...
 
template<typename... Args, typename = typename std::enable_if<std::is_constructible<E,Args...>::value>::type>
constexpr result (in_place_error_t, Args &&... args) noexcept(std::is_nothrow_constructible< E, Args... >::value)
 Constructs a result object that contains an error. More...
 
template<typename U , typename... Args, typename = typename std::enable_if<std::is_constructible<E, std::initializer_list<U>&, Args...>::value>::type>
constexpr result (in_place_error_t, std::initializer_list< U > ilist, Args &&...args) noexcept(std::is_nothrow_constructible< E, std::initializer_list< U >, Args... >::value)
 Constructs a result object that contains an error. More...
 
auto operator= (const result &other) -> result &=default
 Copy assigns the result stored in other. More...
 
auto operator= (result &&other) -> result &=default
 Move assigns the result stored in other. More...
 
template<typename E2 , typename = typename std::enable_if<std::is_nothrow_constructible<E,const E2&>::value && std::is_assignable<E&,const E2&>::value>::type>
auto operator= (const result< void, E2 > &other) noexcept(std::is_nothrow_assignable< E, const E2 &>::value) -> result &
 Copy-converts the state of other. More...
 
template<typename E2 , typename = typename std::enable_if<std::is_nothrow_constructible<E,E2&&>::value && std::is_assignable<E&,E2&&>::value>::type>
auto operator= (result< void, E2 > &&other) noexcept(std::is_nothrow_assignable< E, E2 &&>::value) -> result &
 Move-converts the state of other. More...
 
RESULT_WARN_UNUSED constexpr operator bool () const noexcept
 Contextually convertible to true if *this does not contain an error. More...
 
RESULT_WARN_UNUSED constexpr auto has_value () const noexcept -> bool
 Returns true if *this contains a value. More...
 
RESULT_WARN_UNUSED constexpr auto has_error () const noexcept -> bool
 Returns true if *this contains an error. More...
 
template<typename U >
RESULT_WARN_UNUSED constexpr auto and_then (U &&value) const -> result< typename std::decay< U >::type, E >
 Returns a result containing value if this result contains a value, otherwise returns a result containing the current error. More...
 
template<typename E2 , typename = typename std::enable_if<std::is_constructible<E,const E2&>::value>::type>
constexpr result (const failure< E2 > &e) noexcept(std::is_nothrow_constructible< E, const E2 &>::value)
 Constructs the underlying error of this result. More...
 
template<typename E2 , typename = typename std::enable_if<std::is_constructible<E,E2&&>::value>::type>
constexpr result (failure< E2 > &&e) noexcept(std::is_nothrow_constructible< E, E2 &&>::value)
 Constructs the underlying error of this result. More...
 
template<typename E2 , typename = typename std::enable_if<detail::result_is_failure_assignable<E,const E2&>::value>::type>
auto operator= (const failure< E2 > &other) noexcept(std::is_nothrow_assignable< E, const E2 &>::value) -> result &
 Perfect-forwarded assignment. More...
 
template<typename E2 , typename = typename std::enable_if<detail::result_is_failure_assignable<E,E2&&>::value>::type>
auto operator= (failure< E2 > &&other) noexcept(std::is_nothrow_assignable< E, E2 &&>::value) -> result &
 Perfect-forwarded assignment. More...
 
RESULT_CPP14_CONSTEXPR auto value () &&-> void
 Throws an exception if (*this) is in an error state. More...
 
RESULT_CPP14_CONSTEXPR auto value () const &-> void
 Throws an exception if (*this) is in an error state. More...
 
RESULT_WARN_UNUSED constexpr auto error () const &noexcept(std::is_nothrow_constructible< E >::value &&std::is_nothrow_copy_constructible< E >::value) -> E
 Returns the contained error, if one exists, or a default-constructed error value. More...
 
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto error () &&noexcept(std::is_nothrow_constructible< E >::value &&std::is_nothrow_copy_constructible< E >::value) -> E
 Returns the contained error, if one exists, or a default-constructed error value. More...
 
template<typename String , typename = typename std::enable_if<( std::is_convertible<String,const std::string&>::value && std::is_copy_constructible<E>::value )>::type>
RESULT_CPP14_CONSTEXPR auto expect (String &&message) const &-> void
 } More...
 
template<typename String , typename = typename std::enable_if<( std::is_convertible<String,const std::string&>::value && std::is_move_constructible<E>::value )>::type>
RESULT_CPP14_CONSTEXPR auto expect (String &&message) &&-> void
 } More...
 
template<typename U >
RESULT_WARN_UNUSED constexpr auto error_or (U &&default_error) const &-> error_type
 Returns the contained error if *this has an error, otherwise returns default_error. More...
 
template<typename U >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto error_or (U &&default_error) &&-> error_type
 Returns the contained error if *this has an error, otherwise returns default_error. More...
 
template<typename Fn >
RESULT_WARN_UNUSED constexpr auto flat_map (Fn &&fn) const &-> detail::invoke_result_t< Fn >
 Invokes the function fn if (*this) contains no value. More...
 
template<typename Fn >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto flat_map (Fn &&fn) &&-> detail::invoke_result_t< Fn >
 Invokes the function fn if (*this) contains no value. More...
 
template<typename Fn >
RESULT_WARN_UNUSED constexpr auto map (Fn &&fn) const &-> result< detail::invoke_result_t< Fn >, E >
 Invokes the function fn if (*this) contains no value. More...
 
template<typename Fn >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto map (Fn &&fn) &&-> result< detail::invoke_result_t< Fn >, E >
 Invokes the function fn if (*this) contains no value. More...
 
template<typename Fn >
constexpr auto map_error (Fn &&fn) const &-> result< void, detail::invoke_result_t< Fn, const E &>>
 Invokes the function fn with the error of this result as the argument. More...
 
template<typename Fn >
RESULT_CPP14_CONSTEXPR auto map_error (Fn &&fn) &&-> result< void, detail::invoke_result_t< Fn, E &&>>
 Invokes the function fn with the error of this result as the argument. More...
 
template<typename Fn >
RESULT_WARN_UNUSED constexpr auto flat_map_error (Fn &&fn) const &-> detail::invoke_result_t< Fn, const E &>
 Invokes the function fn with the error of this result as the argument. More...
 
template<typename Fn >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto flat_map_error (Fn &&fn) &&-> detail::invoke_result_t< Fn, E &&>
 Invokes the function fn with the error of this result as the argument. More...
 

Friends

template<typename T2 , typename E2 >
class result
 

Detailed Description

template<typename E>
class cpp::bitwizeshift::result< void, E >

Partial specialization of result<void, E>

Template Parameters
Ethe underlying error type

Definition at line 2659 of file result.hpp.

Constructor & Destructor Documentation

◆ result() [1/10]

template<typename E >
constexpr cpp::bitwizeshift::result< void, E >::result ( )
noexcept

Constructs a result object in a value state.

Examples

Basic Usage:

auto r = cpp::result<void,int>{};

◆ result() [2/10]

template<typename E >
constexpr cpp::bitwizeshift::result< void, E >::result ( const result< void, E > &  other)
default

Copy constructs this result.

If other contains an error, constructs an object that contains a copy of that error.

Note
This constructor is defined as deleted if std::is_copy_constructible<E>::value is false
This constructor is defined as trivial if both std::is_trivially_copy_constructible<E>::value are true

Examples

Basic Usage:

const auto r = cpp::result<void,int>{};
const auto s = r;
Parameters
otherthe result to copy

◆ result() [3/10]

template<typename E >
constexpr cpp::bitwizeshift::result< void, E >::result ( result< void, E > &&  other)
default

Move constructs a result.

If other contains an error, move-constructs this result from that error.

Note
This constructor is defined as deleted if std::is_move_constructible<E>::value is false
This constructor is defined as trivial if both std::is_trivially_move_constructible<E>::value are true

Examples

Basic Usage:

auto r = cpp::result<void,std::string>{};
auto s = std::move(r);
Parameters
otherthe result to move

◆ result() [4/10]

template<typename E >
template<typename U , typename E2 , typename = typename std::enable_if<std::is_constructible<E,E2>::value>::type>
cpp::bitwizeshift::result< void, E >::result ( const result< U, E2 > &  other) const &
explicitnoexcept

Converting copy constructor.

If other contains a value, constructs a result object that is not in an error state – ignoring the value.

If other contains an error, constructs a result object that contains an error, initialized as if direct-initializing (but not direct-list-initializing) an object of type E.

Note
This constructor does not participate in overload resolution unless the following conditions are met:
  • std::is_constructible_v<E, const E2&> is true

Examples

Basic Usage:

const auto r = cpp::result<int,int>{42};
const auto s = cpp::result<void,int>{r};
Parameters
otherthe other type to convert

◆ result() [5/10]

template<typename E >
template<typename U , typename E2 , typename = typename std::enable_if<std::is_constructible<E,E2>::value>::type>
cpp::bitwizeshift::result< void, E >::result ( result< U, E2 > &&  other) const &&
explicitnoexcept

Converting move constructor.

If other contains an error, constructs a result object that contains an error, initialized as if direct-initializing (but not direct-list-initializing) an object of type E&&.

Note
This constructor does not participate in overload resolution unless the following conditions are met:
  • std::is_constructible_v<E, const E2&> is true

Examples

Basic Usage:

auto r = cpp::result<int,std::string>{42};
auto s = cpp::result<void,std::string>{
std::move(r)
};
Parameters
otherthe other type to convert

◆ result() [6/10]

template<typename E >
constexpr cpp::bitwizeshift::result< void, E >::result ( in_place_t  )
explicitnoexcept

Constructs a result object in a value state.

This constructor exists primarily for symmetry with the result<T,E> constructor. Unlike the T overload, no variadic arguments may be supplied.

Examples

Basic Usage:

auto r = cpp::result<void,std::string>{cpp::in_place};

◆ result() [7/10]

template<typename E >
template<typename... Args, typename = typename std::enable_if<std::is_constructible<E,Args...>::value>::type>
constexpr cpp::bitwizeshift::result< void, E >::result ( in_place_error_t  ,
Args &&...  args 
) const
explicitnoexcept

Constructs a result object that contains an error.

the value is initialized as if direct-initializing (but not direct-list-initializing) an object of type E from the arguments std::forward<Args>(args)...

Examples

Basic Usage:

auto r = cpp::result<void,std::string>{
cpp::in_place_error, "Hello world"
};
Parameters
argsthe arguments to pass to E's constructor

◆ result() [8/10]

template<typename E >
template<typename U , typename... Args, typename = typename std::enable_if<std::is_constructible<E, std::initializer_list<U>&, Args...>::value>::type>
constexpr cpp::bitwizeshift::result< void, E >::result ( in_place_error_t  ,
std::initializer_list< U >  ilist,
Args &&...  args 
) const
explicitnoexcept

Constructs a result object that contains an error.

The value is initialized as if direct-initializing (but not direct-list-initializing) an object of type E from the arguments std::forward<std::initializer_list<U>>(ilist), std::forward<Args>(args)...

Examples

Basic Usage:

auto r = cpp::result<void,std::string>{
cpp::in_place_error, {'H','e','l','l','o'}
};
Parameters
ilistAn initializer list of entries to forward
argsthe arguments to pass to Es constructor

◆ result() [9/10]

template<typename E >
template<typename E2 , typename = typename std::enable_if<std::is_constructible<E,const E2&>::value>::type>
constexpr cpp::bitwizeshift::result< void, E >::result ( const failure< E2 > &  e) const &
noexcept

Constructs the underlying error of this result.

Note
This constructor only participates in overload resolution if E is constructible from e

Examples

Basic Usage:

cpp::result<void,int> r = cpp::fail(42);
auto get_error_result() -> cpp::result<void,std::string> {
return cpp::fail("hello world!");
}
Parameters
ethe failure error

◆ result() [10/10]

template<typename E >
template<typename E2 , typename = typename std::enable_if<std::is_constructible<E,E2&&>::value>::type>
constexpr cpp::bitwizeshift::result< void, E >::result ( failure< E2 > &&  e) const &&
noexcept

Constructs the underlying error of this result.

Note
This constructor only participates in overload resolution if E is constructible from e

Examples

Basic Usage:

cpp::result<void,int> r = cpp::fail(42);
auto get_error_result() -> cpp::result<void,std::string> {
return cpp::fail("hello world!");
}
Parameters
ethe failure error

Member Function Documentation

◆ and_then()

template<typename E >
template<typename U >
RESULT_WARN_UNUSED constexpr auto cpp::bitwizeshift::result< void, E >::and_then ( U &&  value) const -> result< typename std::decay< U >::type, E >

Returns a result containing value if this result contains a value, otherwise returns a result containing the current error.

Examples

Basic Usage:

auto r = cpp::result<int,int>{42};
assert(r.and_then(100) == 100);
auto r = cpp::result<int,int>{cpp::fail(42)};
assert(r.and_then(100) == cpp::fail(42));
Parameters
valuethe value to return as a result
Returns
a result of value if this contains a value

◆ error() [1/2]

template<typename E >
RESULT_WARN_UNUSED constexpr auto cpp::bitwizeshift::result< void, E >::error ( ) const && -> E
noexcept

Returns the contained error, if one exists, or a default-constructed error value.

The error() function will not throw any exceptions if E does not throw any exceptions for the copy or move construction.

This is to limit the possible scope for exceptions, and to allow the error type to be treated as a "status"-like type, where the default-constructed case is considered the "good" state.

If this function is invoked on an rvalue of a result, the error is returned via move-construction

Requires

  • std::is_default_constructible<E>::value is true
  • std::is_copy_constructible<E>::value or std::is_move_constructible<E>::value is true
  • E{} represents the "good" (non-error) state

Examples

Basic Usage:

auto r = cpp::result<int,std::error_code>{ 42 };
assert(r.error() == std::error_code{});
auto r = cpp::result<int,std::error_code>{
cpp::fail(std::io_errc::stream)
};
assert(r.error() == std::io_errc::stream);
Returns
the error or a default-constructed error value

◆ error() [2/2]

template<typename E >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< void, E >::error ( ) const && -> E
noexcept

Returns the contained error, if one exists, or a default-constructed error value.

The error() function will not throw any exceptions if E does not throw any exceptions for the copy or move construction.

This is to limit the possible scope for exceptions, and to allow the error type to be treated as a "status"-like type, where the default-constructed case is considered the "good" state.

If this function is invoked on an rvalue of a result, the error is returned via move-construction

Requires

  • std::is_default_constructible<E>::value is true
  • std::is_copy_constructible<E>::value or std::is_move_constructible<E>::value is true
  • E{} represents the "good" (non-error) state

Examples

Basic Usage:

auto r = cpp::result<int,std::error_code>{ 42 };
assert(r.error() == std::error_code{});
auto r = cpp::result<int,std::error_code>{
cpp::fail(std::io_errc::stream)
};
assert(r.error() == std::io_errc::stream);
Returns
the error or a default-constructed error value

◆ error_or() [1/2]

template<typename E >
template<typename U >
RESULT_WARN_UNUSED constexpr auto cpp::bitwizeshift::result< void, E >::error_or ( U &&  default_error) const & -> error_type

Returns the contained error if *this has an error, otherwise returns default_error.

Examples

Basic Usage:

auto r = cpp::result<int,int>{42};
assert(r.error_or(0) == cpp::fail(0));
auto r = cpp::result<int,int>{cpp::fail(42)};
assert(r.error_or(0) == cpp::fail(42));
Parameters
default_errorthe error to use in case *this is empty
Returns
the contained value or default_error

◆ error_or() [2/2]

template<typename E >
template<typename U >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< void, E >::error_or ( U &&  default_error) && -> error_type

Returns the contained error if *this has an error, otherwise returns default_error.

Examples

Basic Usage:

auto r = cpp::result<int,int>{42};
assert(r.error_or(0) == cpp::fail(0));
auto r = cpp::result<int,int>{cpp::fail(42)};
assert(r.error_or(0) == cpp::fail(42));
Parameters
default_errorthe error to use in case *this is empty
Returns
the contained value or default_error

◆ expect() [1/2]

template<typename E >
template<typename String , typename = typename std::enable_if<( std::is_convertible<String,const std::string&>::value && std::is_copy_constructible<E>::value )>::type>
RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< void, E >::expect ( String &&  message) const & -> void

}

Asserts an expectation that this result contains an error, throwing a bad_result_access on failure

If this function is invoked from an rvalue of result, then this will consume the underlying error first, if there is one.

Note
This function exists as a means to allow for results to be marked used without requiring directly inspecting the underlying value. This is, in effect, equivalent to assert(res.has_value()), however it uses exceptions to ensure the stack can be unwound, and exceptions invoked.

Examples

Basic Usage:

auto start_service() -> cpp::result<void,int>;
start_service().expect("Service failed to start!");
Parameters
messagethe message to provide to this expectation

◆ expect() [2/2]

template<typename E >
template<typename String , typename = typename std::enable_if<( std::is_convertible<String,const std::string&>::value && std::is_move_constructible<E>::value )>::type>
RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< void, E >::expect ( String &&  message) && -> void

}

Asserts an expectation that this result contains an error, throwing a bad_result_access on failure

If this function is invoked from an rvalue of result, then this will consume the underlying error first, if there is one.

Note
This function exists as a means to allow for results to be marked used without requiring directly inspecting the underlying value. This is, in effect, equivalent to assert(res.has_value()), however it uses exceptions to ensure the stack can be unwound, and exceptions invoked.

Examples

Basic Usage:

auto start_service() -> cpp::result<void,int>;
start_service().expect("Service failed to start!");
Parameters
messagethe message to provide to this expectation

◆ flat_map() [1/2]

template<typename E >
template<typename Fn >
RESULT_WARN_UNUSED constexpr auto cpp::bitwizeshift::result< void, E >::flat_map ( Fn &&  fn) const & -> detail::invoke_result_t< Fn >

Invokes the function fn if (*this) contains no value.

If this result contains an error, a result of the error is returned

The function being called must return a result type or the program is ill-formed

If this is called on an rvalue of result which contains an error, the returned result is constructed from an rvalue of that error.

Examples

Basic Usage:

auto generate_int() -> cpp::result<int,int> { return 42; }
auto r = cpp::result<void,int>{};
assert(r.flat_map(generate_int) == 42);
auto r = cpp::result<void,int>{cpp::fail(42)};
assert(r.flat_map(generate_int) == cpp::fail(42));
Parameters
fnthe function to invoke with this
Returns
The result of the function being called

◆ flat_map() [2/2]

template<typename E >
template<typename Fn >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< void, E >::flat_map ( Fn &&  fn) && -> detail::invoke_result_t< Fn >

Invokes the function fn if (*this) contains no value.

If this result contains an error, a result of the error is returned

The function being called must return a result type or the program is ill-formed

If this is called on an rvalue of result which contains an error, the returned result is constructed from an rvalue of that error.

Examples

Basic Usage:

auto generate_int() -> cpp::result<int,int> { return 42; }
auto r = cpp::result<void,int>{};
assert(r.flat_map(generate_int) == 42);
auto r = cpp::result<void,int>{cpp::fail(42)};
assert(r.flat_map(generate_int) == cpp::fail(42));
Parameters
fnthe function to invoke with this
Returns
The result of the function being called

◆ flat_map_error() [1/2]

template<typename E >
template<typename Fn >
RESULT_WARN_UNUSED constexpr auto cpp::bitwizeshift::result< void, E >::flat_map_error ( Fn &&  fn) const & -> detail::invoke_result_t< Fn, const E &>

Invokes the function fn with the error of this result as the argument.

If this result contains a value, a result of the value is returned

The function being called must return a result type or the program is ill-formed

If this is called on an rvalue of result which contains an error, the returned result is constructed from an rvalue of that error.

Examples

Basic Usage:

auto to_string(int) -> cpp::result<int,std::string>;
auto r = cpp::result<int,int>{42};
assert(r.flat_map(to_string) == 42);
auto r = cpp::result<int,int>{cpp::fail(42)};
assert(r.flat_map(to_string) == cpp::fail("42"));
Parameters
fnthe function to invoke with this
Returns
The result of the function being called

◆ flat_map_error() [2/2]

template<typename E >
template<typename Fn >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< void, E >::flat_map_error ( Fn &&  fn) && -> detail::invoke_result_t< Fn, E &&>

Invokes the function fn with the error of this result as the argument.

If this result contains a value, a result of the value is returned

The function being called must return a result type or the program is ill-formed

If this is called on an rvalue of result which contains an error, the returned result is constructed from an rvalue of that error.

Examples

Basic Usage:

auto to_string(int) -> cpp::result<int,std::string>;
auto r = cpp::result<int,int>{42};
assert(r.flat_map(to_string) == 42);
auto r = cpp::result<int,int>{cpp::fail(42)};
assert(r.flat_map(to_string) == cpp::fail("42"));
Parameters
fnthe function to invoke with this
Returns
The result of the function being called

◆ has_error()

template<typename E >
RESULT_WARN_UNUSED constexpr auto cpp::bitwizeshift::result< void, E >::has_error ( ) const -> bool
noexcept

Returns true if *this contains an error.

Examples

Basic Usage:

auto get_result() -> cpp::result<int, int>;
auto r = get_result();
if (r.has_error()) { ... }
assert(!cpp::result<int,int>{42}.has_error());
assert(cpp::result<int,int>{cpp::fail(42)}.has_error());
Returns
true if *this contains an error, false if *this contains a value

◆ has_value()

template<typename E >
RESULT_WARN_UNUSED constexpr auto cpp::bitwizeshift::result< void, E >::has_value ( ) const -> bool
noexcept

Returns true if *this contains a value.

Examples

Basic Usage:

auto get_result() -> cpp::result<int, int>;
auto r = get_result();
if (r.has_value()) { ... }
assert(cpp::result<int,int>{42}.has_value());
assert(!cpp::result<int,int>{cpp::fail(42)}.has_value());
Returns
true if *this contains a value, false if *this contains an error

◆ map() [1/2]

template<typename E >
template<typename Fn >
RESULT_WARN_UNUSED constexpr auto cpp::bitwizeshift::result< void, E >::map ( Fn &&  fn) const & -> result< detail::invoke_result_t< Fn >, E >

Invokes the function fn if (*this) contains no value.

If this result is an error, the result of this function is that error. Otherwise this function wraps the result and returns it as an result.

If this is called on an rvalue of result which contains an error, the returned result is constructed from an rvalue of that error.

Examples

Basic Usage:

auto generate_int() -> int { return 42; }
auto r = cpp::result<void,int>{};
assert(r.map(generate_int) == 42);
auto r = cpp::result<void,int>{cpp::fail(42)};
assert(r.map(generate_int) == cpp::fail(42));
Parameters
fnthe function to invoke with this
Returns
The result result of the function invoked

◆ map() [2/2]

template<typename E >
template<typename Fn >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< void, E >::map ( Fn &&  fn) && -> result< detail::invoke_result_t< Fn >, E >

Invokes the function fn if (*this) contains no value.

If this result is an error, the result of this function is that error. Otherwise this function wraps the result and returns it as an result.

If this is called on an rvalue of result which contains an error, the returned result is constructed from an rvalue of that error.

Examples

Basic Usage:

auto generate_int() -> int { return 42; }
auto r = cpp::result<void,int>{};
assert(r.map(generate_int) == 42);
auto r = cpp::result<void,int>{cpp::fail(42)};
assert(r.map(generate_int) == cpp::fail(42));
Parameters
fnthe function to invoke with this
Returns
The result result of the function invoked

◆ map_error() [1/2]

template<typename E >
template<typename Fn >
constexpr auto cpp::bitwizeshift::result< void, E >::map_error ( Fn &&  fn) const & -> result< void, detail::invoke_result_t< Fn, const E &>>

Invokes the function fn with the error of this result as the argument.

If this result contains a value, the result of this function is that value. Otherwise the function is called with that error and returns the result as a result.

If this is called on an rvalue of result which contains a value, the returned result is constructed from an rvalue of that value.

Examples

Basic Usage:

auto to_string(int) -> std::string;
auto r = cpp::result<int,int>{42};
assert(r.map_error(to_string) == 42);
auto r = cpp::result<int,int>{cpp::fail(42)};
assert(r.map_error(to_string) == cpp::fail("42"));
auto r = cpp::result<std::string,int>{};
auto s = r.map(std::string::size); // 's' contains 'result<size_t,int>'
Parameters
fnthe function to invoke with this
Returns
The result result of the function invoked

◆ map_error() [2/2]

template<typename E >
template<typename Fn >
RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< void, E >::map_error ( Fn &&  fn) && -> result< void, detail::invoke_result_t< Fn, E &&>>

Invokes the function fn with the error of this result as the argument.

If this result contains a value, the result of this function is that value. Otherwise the function is called with that error and returns the result as a result.

If this is called on an rvalue of result which contains a value, the returned result is constructed from an rvalue of that value.

Examples

Basic Usage:

auto to_string(int) -> std::string;
auto r = cpp::result<int,int>{42};
assert(r.map_error(to_string) == 42);
auto r = cpp::result<int,int>{cpp::fail(42)};
assert(r.map_error(to_string) == cpp::fail("42"));
auto r = cpp::result<std::string,int>{};
auto s = r.map(std::string::size); // 's' contains 'result<size_t,int>'
Parameters
fnthe function to invoke with this
Returns
The result result of the function invoked

◆ operator bool()

template<typename E >
RESULT_WARN_UNUSED constexpr cpp::bitwizeshift::result< void, E >::operator bool ( ) const
explicitnoexcept

Contextually convertible to true if *this does not contain an error.

This function exists to allow for simple, terse checks for containing a value.

Examples

Basic Usage:

auto get_result() -> cpp::result<void, int>;
auto r = get_result();
if (r) { ... }
assert(static_cast<bool>(cpp::result<void,int>{}));
assert(!static_cast<bool>(cpp::result<void,int>{cpp::fail(42)}));
Returns
true if *this contains a value, false if *this does not contain a value

◆ operator=() [1/6]

template<typename E >
auto cpp::bitwizeshift::result< void, E >::operator= ( const result< void, E > &  other) -> result &=default
default

Copy assigns the result stored in other.

Note
The function does not participate in overload resolution unless
  • std::is_nothrow_copy_constructible_v<E> is true this restriction guarantees that no 'valueless_by_exception` state may occur.
This assignment operator is defined as trivial if the following conditions are all true:
  • std::is_trivially_copy_constructible<E>::value
  • std::is_trivially_copy_assignable<E>::value
  • std::is_trivially_destructible<E>::value
Parameters
otherthe other result to copy

◆ operator=() [2/6]

template<typename E >
auto cpp::bitwizeshift::result< void, E >::operator= ( result< void, E > &&  other) -> result &=default
default

Move assigns the result stored in other.

Note
The function does not participate in overload resolution unless
  • std::is_nothrow_copy_constructible_v<E> is true this restriction guarantees that no 'valueless_by_exception` state may occur.
This assignment operator is defined as trivial if the following conditions are all true:
  • std::is_trivially_move_constructible<E>::value
  • std::is_trivially_move_assignable<E>::value
  • std::is_trivially_destructible<E>::value
Parameters
otherthe other result to copy

◆ operator=() [3/6]

template<typename E >
template<typename E2 , typename = typename std::enable_if<std::is_nothrow_constructible<E,const E2&>::value && std::is_assignable<E&,const E2&>::value>::type>
auto cpp::bitwizeshift::result< void, E >::operator= ( const result< void, E2 > &  other) const & -> result &
noexcept

Copy-converts the state of other.

If both this and other contain an error, the underlying error is assigned through copy-assignment.

If other contains a value state, this result is constructed in a value state.

If other contans an error state, and this contains a value state, the underlying error is constructed through copy-construction.

Note
The function does not participate in overload resolution unless
  • std::is_nothrow_constructible_v<E, const E2&>, std::is_assignable_v<E&, const E2&> are all true.
Parameters
otherthe other result object to convert
Returns
reference to (*this)

◆ operator=() [4/6]

template<typename E >
template<typename E2 , typename = typename std::enable_if<std::is_nothrow_constructible<E,E2&&>::value && std::is_assignable<E&,E2&&>::value>::type>
auto cpp::bitwizeshift::result< void, E >::operator= ( result< void, E2 > &&  other) && -> result &
noexcept

Move-converts the state of other.

If both this and other contain an error, the underlying error is assigned through move-assignment.

If other contains a value state, this result is constructed in a value state.

If other contans an error state, and this contains a value state, the underlying error is constructed through move-construction.

Note
The function does not participate in overload resolution unless
  • std::is_nothrow_constructible_v<E, E2&&>, std::is_assignable_v<E&, E2&&> are all true.
Parameters
otherthe other result object to convert
Returns
reference to (*this)

◆ operator=() [5/6]

template<typename E >
template<typename E2 , typename = typename std::enable_if<detail::result_is_failure_assignable<E,const E2&>::value>::type>
auto cpp::bitwizeshift::result< void, E >::operator= ( const failure< E2 > &  other) const & -> result &
noexcept

Perfect-forwarded assignment.

Depending on whether *this contains an error before the call, the contained error is either direct-initialized via forwarding the error, or assigned from forwarding the error

Note
The function does not participate in overload resolution unless
  • std::is_nothrow_constructible_v<E, E2> is true, and
  • std::is_assignable_v<E&, E2> is true
Parameters
otherthe failure value to assign to this
Returns
reference to (*this)

◆ operator=() [6/6]

template<typename E >
template<typename E2 , typename = typename std::enable_if<detail::result_is_failure_assignable<E,E2&&>::value>::type>
auto cpp::bitwizeshift::result< void, E >::operator= ( failure< E2 > &&  other) && -> result &
noexcept

Perfect-forwarded assignment.

Depending on whether *this contains an error before the call, the contained error is either direct-initialized via forwarding the error, or assigned from forwarding the error

Note
The function does not participate in overload resolution unless
  • std::is_nothrow_constructible_v<E, E2> is true, and
  • std::is_assignable_v<E&, E2> is true
Parameters
otherthe failure value to assign to this
Returns
reference to (*this)

◆ value() [1/2]

template<typename E >
RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< void, E >::value ( ) && -> void

Throws an exception if (*this) is in an error state.

This function exists for symmetry with cpp::result<T,E> objects where T contains a value.

If this contains an error, an exception is thrown containing the underlying error. The error is consumed propagating the same constness and refness of this result.

Examples

Basic Usage:

cpp::result<void,int>{}.value(); // no exception
auto r = cpp::result<void,std::unique_ptr<int>>{
cpp::fail(std::make_unique<int>(42))
};
std::move(r).value(); // throws bad_result_access<std::unique_ptr<int>>
try {
auto r = cpp::result<void,int>{ cpp::fail(42) }.value();
} catch (const cpp::bad_result_access<int>& e) {
assert(e.error() == 42);
}
Exceptions
bad_result_access<E>if *this does not contain a value.

◆ value() [2/2]

template<typename E >
RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< void, E >::value ( ) const & -> void

Throws an exception if (*this) is in an error state.

This function exists for symmetry with cpp::result<T,E> objects where T contains a value.

If this contains an error, an exception is thrown containing the underlying error. The error is consumed propagating the same constness and refness of this result.

Examples

Basic Usage:

cpp::result<void,int>{}.value(); // no exception
auto r = cpp::result<void,std::unique_ptr<int>>{
cpp::fail(std::make_unique<int>(42))
};
std::move(r).value(); // throws bad_result_access<std::unique_ptr<int>>
try {
auto r = cpp::result<void,int>{ cpp::fail(42) }.value();
} catch (const cpp::bad_result_access<int>& e) {
assert(e.error() == 42);
}
Exceptions
bad_result_access<E>if *this does not contain a value.

The documentation for this class was generated from the following file: