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

The class template result manages result results from APIs, while encoding possible failure conditions. More...

#include <result.hpp>

Public Types

using value_type = T
 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

template<typename U = T, typename = typename std::enable_if<std::is_constructible<U>::value>::type>
constexpr result () noexcept(std::is_nothrow_constructible< U >::value)
 Default-constructs a result with the underlying value type active. More...
 
constexpr result (const result &other)=default
 Copy constructs this result. More...
 
constexpr result (result &&other)=default
 Move constructs a result. More...
 
template<typename... Args, typename = typename std::enable_if<std::is_constructible<T,Args...>::value>::type>
constexpr result (in_place_t, Args &&... args) noexcept(std::is_nothrow_constructible< T, Args... >::value)
 Constructs a result object that contains a value. More...
 
template<typename U , typename... Args, typename = typename std::enable_if<std::is_constructible<T, std::initializer_list<U>&, Args...>::value>::type>
constexpr result (in_place_t, std::initializer_list< U > ilist, Args &&...args) noexcept(std::is_nothrow_constructible< T, std::initializer_list< U >, Args... >::value)
 Constructs a result object that contains a value. 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 T2 , typename E2 , typename = typename std::enable_if<detail::result_is_copy_convert_assignable<T,E,T2,E2>::value>::type>
auto operator= (const result< T2, E2 > &other) noexcept(std::is_nothrow_assignable< T, const T2 & >::value &&std::is_nothrow_assignable< E, const E2 & >::value) -> result &
 Copy-converts the state of other. More...
 
template<typename T2 , typename E2 , typename = typename std::enable_if<detail::result_is_move_convert_assignable<T,E,T2,E2>::value>::type>
auto operator= (result< T2, E2 > &&other) noexcept(std::is_nothrow_assignable< T, T2 && >::value &&std::is_nothrow_assignable< E, E2 && >::value) -> result &
 Move-converts the state of other. More...
 
template<typename U , typename = typename std::enable_if<detail::result_is_value_assignable<T,U>::value>::type>
auto operator= (U &&value) noexcept(std::is_nothrow_assignable< T, U >::value) -> result &
 Perfect-forwarded assignment. More...
 
constexpr RESULT_WARN_UNUSED operator bool () const noexcept
 Contextually convertible to true if *this contains a value. More...
 
constexpr RESULT_WARN_UNUSED auto has_value () const noexcept -> bool
 Returns true if *this contains a value. More...
 
constexpr RESULT_WARN_UNUSED auto has_error () const noexcept -> bool
 Returns true if *this contains an error. More...
 
template<typename U >
constexpr RESULT_WARN_UNUSED 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 T2 , typename E2 , typename std::enable_if< detail::result_is_implicit_copy_convertible< T, E, T2, E2 >::value, int >::type = 0>
 result (const result< T2, E2 > &other) noexcept(std::is_nothrow_constructible< T, const T2 & >::value &&std::is_nothrow_constructible< E, const E2 & >::value)
 Converting copy constructor. More...
 
template<typename T2 , typename E2 , typename std::enable_if< detail::result_is_explicit_copy_convertible< T, E, T2, E2 >::value, int >::type = 0>
 result (const result< T2, E2 > &other) noexcept(std::is_nothrow_constructible< T, const T2 & >::value &&std::is_nothrow_constructible< E, const E2 & >::value)
 Converting copy constructor. More...
 
template<typename T2 , typename E2 , typename std::enable_if< detail::result_is_implicit_move_convertible< T, E, T2, E2 >::value, int >::type = 0>
 result (result< T2, E2 > &&other) noexcept(std::is_nothrow_constructible< T, T2 && >::value &&std::is_nothrow_constructible< E, E2 && >::value)
 Converting move constructor. More...
 
template<typename T2 , typename E2 , typename std::enable_if< detail::result_is_explicit_move_convertible< T, E, T2, E2 >::value, int >::type = 0>
 result (result< T2, E2 > &&other) noexcept(std::is_nothrow_constructible< T, T2 && >::value &&std::is_nothrow_constructible< E, E2 && >::value)
 Converting move constructor. 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 U , typename std::enable_if< detail::result_is_explicit_value_convertible< T, U >::value, int >::type = 0>
constexpr result (U &&value) noexcept(std::is_nothrow_constructible< T, U >::value)
 Constructs a result object that contains a value. More...
 
template<typename U , typename std::enable_if< detail::result_is_implicit_value_convertible< T, U >::value, int >::type = 0>
constexpr result (U &&value) noexcept(std::is_nothrow_constructible< T, U >::value)
 Constructs a result object that contains a value. 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_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto operator-> () noexcept -> typename std::remove_reference< T >::type *
 Retrieves a pointer to the contained value. More...
 
constexpr RESULT_WARN_UNUSED auto operator-> () const noexcept -> typename std::remove_reference< typename std::add_const< T >::type >::type *
 Retrieves a pointer to the contained value. More...
 
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto operator* () &noexcept -> typename std::add_lvalue_reference< T >::type
 Retrieves a reference to the contained value. More...
 
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto operator* () &&noexcept -> typename std::add_rvalue_reference< T >::type
 Retrieves a reference to the contained value. More...
 
constexpr RESULT_WARN_UNUSED auto operator* () const &noexcept -> typename std::add_lvalue_reference< typename std::add_const< T >::type >::type
 Retrieves a reference to the contained value. More...
 
constexpr RESULT_WARN_UNUSED auto operator* () const &&noexcept -> typename std::add_rvalue_reference< typename std::add_const< T >::type >::type
 Retrieves a reference to the contained value. More...
 
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto value () &-> typename std::add_lvalue_reference< T >::type
 Returns a reference to the contained value. More...
 
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto value () &&-> typename std::add_rvalue_reference< T >::type
 Returns a reference to the contained value. More...
 
constexpr RESULT_WARN_UNUSED auto value () const &-> typename std::add_lvalue_reference< typename std::add_const< T >::type >::type
 Returns a reference to the contained value. More...
 
constexpr RESULT_WARN_UNUSED auto value () const &&-> typename std::add_rvalue_reference< typename std::add_const< T >::type >::type
 Returns a reference to the contained value. More...
 
constexpr RESULT_WARN_UNUSED 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_move_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) &-> typename std::add_lvalue_reference< T >::type
 } 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) &&-> typename std::add_rvalue_reference< T >::type
 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 &-> typename std::add_lvalue_reference< typename std::add_const< T >::type >::type
 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 &&-> typename std::add_rvalue_reference< typename std::add_const< T >::type >::type
 Returns the contained error, if one exists, or a default-constructed error value. More...
 
template<typename U >
constexpr RESULT_WARN_UNUSED auto value_or (U &&default_value) const &-> typename std::remove_reference< T >::type
 Returns the contained value if *this has a value, otherwise returns default_value. More...
 
template<typename U >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto value_or (U &&default_value) &&-> typename std::remove_reference< T >::type
 Returns the contained value if *this has a value, otherwise returns default_value. More...
 
template<typename U >
constexpr RESULT_WARN_UNUSED 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 >
constexpr RESULT_WARN_UNUSED auto flat_map (Fn &&fn) const &-> detail::invoke_result_t< Fn, const T & >
 Invokes the function fn with the value of this result as the argument. More...
 
template<typename Fn >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto flat_map (Fn &&fn) &&-> detail::invoke_result_t< Fn, T && >
 Invokes the function fn with the value of this result as the argument. More...
 
template<typename Fn >
constexpr RESULT_WARN_UNUSED auto map (Fn &&fn) const &-> result< detail::invoke_result_t< Fn, const T & >, E >
 Invokes the function fn with the value of this result as the argument. More...
 
template<typename Fn >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto map (Fn &&fn) &&-> result< detail::invoke_result_t< Fn, T && >, E >
 Invokes the function fn with the value of this result as the argument. More...
 
template<typename Fn >
constexpr RESULT_WARN_UNUSED auto map_error (Fn &&fn) const &-> result< T, 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 map_error (Fn &&fn) &&-> result< T, detail::invoke_result_t< Fn, E && >>
 Invokes the function fn with the error of this result as the argument. More...
 
template<typename Fn >
constexpr RESULT_WARN_UNUSED 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...
 

Detailed Description

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

The class template result manages result results from APIs, while encoding possible failure conditions.

A common use-case for result is the return value of a function that may fail. As opposed to other approaches, such as std::pair<T,bool> or std::optional, result more accurately conveys the intent of the user along with the failure condition to the caller. This effectively produces an orthogonal error handling mechanism that allows for exception safety while also allowing discrete testability of the return type.

result<T,E> types may contain a T value, which signifies that an operation succeeded in producing the result value of type T. If an result does not contain a T value, it will always contain an E error condition instead.

An result<T,E> can always be queried for a possible error case by calling the error() function, even if it contains a value. In the case that a result<T,E> contains a value object, this will simply return an E object constructed through default aggregate construction, as if through the expression E{}, which is assumed to be a "valid" (no-error) state for an E type. For example:

  • std::error_code{} produces a default-construct error-code, which is the "no error" state,
  • integral (or enum) error codes produce a 0 value (no error), thanks to zero-initialization,
  • std::exception_ptr{} produces a null-pointer,
  • std::string{} produces an empty string "",
  • etc.

When a result<T,E> contains either a value or error, the storage for that object is guaranteed to be allocated as part of the result object's footprint, i.e. no dynamic memory allocation ever takes place. Thus, a result object models an object, not a pointer, even though the operator*() and operator->() are defined.

When an object of type result<T,E> is contextually converted to bool, the conversion returns true if the object contains a value and false if it contains an error.

result objects do not have a "valueless" state like variants do. Once a result has been constructed with a value or error, the active underlying type can only be changed through assignment which may is only enabled if construction is guaranteed to be non-throwing. This ensures that a valueless state cannot occur naturally.

Example Use:

auto to_string(int x) -> result<std::string>
{
try {
return std::stoi(x);
} catch (const std::invalid_argument&) {
return fail(std::errc::invalid_argument);
} catch (const std::std::out_of_range&) {
return fail(std::errc::result_out_of_range);
}
}
Note
If using C++17 or above, fail can be replaced with failure{...} thanks to CTAD.
Template Parameters
Tthe underlying value type
Ethe underlying error type

Definition at line 309 of file result.hpp.

Constructor & Destructor Documentation

◆ result() [1/15]

template<typename T , typename E >
template<typename U = T, typename = typename std::enable_if<std::is_constructible<U>::value>::type>
constexpr cpp::bitwizeshift::result< T, E >::result ( ) const
constexprnoexcept

Default-constructs a result with the underlying value type active.

This constructor is only enabled if T is default-constructible

Examples

Basic Usage:

assert(cpp::result<std::string,int>{} == std::string{});

◆ result() [2/15]

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

Copy constructs this result.

If other contains a value, initializes the contained value as if direct-initializing (but not direct-list-initializing) an object of type T with the expression *other.

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<T>::value or std::is_copy_constructible<E>::value is false
This constructor is defined as trivial if both std::is_trivially_copy_constructible<T>::value and std::is_trivially_copy_constructible<E>::value are true

Examples

Basic Usage:

const auto r = cpp::result<int,int>{42};
const auto s = r;
assert(r == s);
Parameters
otherthe result to copy

◆ result() [3/15]

template<typename T , typename E >
constexpr cpp::bitwizeshift::result< T, E >::result ( result< T, E > &&  other)
constexprdefault

Move constructs a result.

If other contains a value, initializes the contained value as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(*other) and does not make other empty: a moved-from result still contains a value, but the value itself is moved from.

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

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

Examples

Basic Usage:

auto r = cpp::result<std::string,int>{"hello world"};
auto s = std::move(r);
assert(s == "hello world");
Parameters
otherthe result to move

◆ result() [4/15]

template<typename T , typename E >
template<typename T2 , typename E2 , typename std::enable_if< detail::result_is_implicit_copy_convertible< T, E, T2, E2 >::value, int >::type = 0>
cpp::bitwizeshift::result< T, E >::result ( const result< T2, E2 > &  other) const &
noexcept

Converting copy constructor.

If other contains a value, constructs a result object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression *other.

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<T, const U&> is true
  • T is not constructible or convertible from any expression of type (possibly const) result<T2,E2>
  • E is not constructible or convertible from any expression of type (possible const) result<T2,E2>
This constructor is explicit if and only if std::is_convertible_v<const T2&, T> or std::is_convertible_v<const E2&, E> is false

Examples

Basic Usage:

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

◆ result() [5/15]

template<typename T , typename E >
template<typename T2 , typename E2 , typename std::enable_if< detail::result_is_explicit_copy_convertible< T, E, T2, E2 >::value, int >::type = 0>
cpp::bitwizeshift::result< T, E >::result ( const result< T2, E2 > &  other) const &
explicitnoexcept

Converting copy constructor.

If other contains a value, constructs a result object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression *other.

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<T, const U&> is true
  • T is not constructible or convertible from any expression of type (possibly const) result<T2,E2>
  • E is not constructible or convertible from any expression of type (possible const) result<T2,E2>
This constructor is explicit if and only if std::is_convertible_v<const T2&, T> or std::is_convertible_v<const E2&, E> is false

Examples

Basic Usage:

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

◆ result() [6/15]

template<typename T , typename E >
template<typename T2 , typename E2 , typename std::enable_if< detail::result_is_implicit_move_convertible< T, E, T2, E2 >::value, int >::type = 0>
cpp::bitwizeshift::result< T, E >::result ( result< T2, E2 > &&  other) const &&
noexcept

Converting move constructor.

If other contains a value, constructs a result object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(*other).

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<T, const U&> is true
  • T is not constructible or convertible from any expression of type (possibly const) result<T2,E2>
  • E is not constructible or convertible from any expression of type (possible const) result<T2,E2>
This constructor is explicit if and only if std::is_convertible_v<const T2&, T> or std::is_convertible_v<const E2&, E> is false

Examples

Basic Usage:

std::make_unique<Derived>()
};
const auto s = cpp::result<std::unique_ptr<Base>,long>{
std::move(r)
};
Parameters
otherthe other type to convert

◆ result() [7/15]

template<typename T , typename E >
template<typename T2 , typename E2 , typename std::enable_if< detail::result_is_explicit_move_convertible< T, E, T2, E2 >::value, int >::type = 0>
cpp::bitwizeshift::result< T, E >::result ( result< T2, E2 > &&  other) const &&
explicitnoexcept

Converting move constructor.

If other contains a value, constructs a result object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(*other).

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<T, const U&> is true
  • T is not constructible or convertible from any expression of type (possibly const) result<T2,E2>
  • E is not constructible or convertible from any expression of type (possible const) result<T2,E2>
This constructor is explicit if and only if std::is_convertible_v<const T2&, T> or std::is_convertible_v<const E2&, E> is false

Examples

Basic Usage:

std::make_unique<Derived>()
};
const auto s = cpp::result<std::unique_ptr<Base>,long>{
std::move(r)
};
Parameters
otherthe other type to convert

◆ result() [8/15]

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

Constructs a result object that contains a value.

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

Examples

Basic Usage:

cpp::in_place, "Hello world"
};
Parameters
argsthe arguments to pass to T's constructor

◆ result() [9/15]

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

Constructs a result object that contains a value.

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

Examples

Basic Usage:

cpp::in_place, {'H','e','l','l','o'}
};
Parameters
ilistAn initializer list of entries to forward
argsthe arguments to pass to T's constructor

◆ result() [10/15]

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

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:

cpp::in_place_error, "Hello world"
};
Parameters
argsthe arguments to pass to E's constructor

◆ result() [11/15]

template<typename T , 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< T, E >::result ( in_place_error_t  ,
std::initializer_list< U >  ilist,
Args &&...  args 
) const
explicitconstexprnoexcept

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:

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() [12/15]

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

Constructs the underlying error of this result.

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

Examples

Basic Usage:

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

◆ result() [13/15]

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

Constructs the underlying error of this result.

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

Examples

Basic Usage:

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

◆ result() [14/15]

template<typename T , typename E >
template<typename U , typename std::enable_if< detail::result_is_explicit_value_convertible< T, U >::value, int >::type = 0>
constexpr cpp::bitwizeshift::result< T, E >::result ( U &&  value) const
explicitconstexprnoexcept

Constructs a result object that contains a value.

The value is initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression value.

Note
This constructor is constexpr if the constructor of T selected by direct-initialization is constexpr
This constructor does not participate in overload resolution unless std::is_constructible_v<T, U&&> is true and decay_t<U> is neither in_place_t, in_place_error_t, nor a result type.
This constructor is explicit if and only if std::is_convertible_v<U&&, T> is false

Examples

Basic Usage:

auto get_value() -> cpp::result<std::string,int> {
return "hello world!"; // implicit conversion
}
Parameters
valuethe value to copy

◆ result() [15/15]

template<typename T , typename E >
template<typename U , typename std::enable_if< detail::result_is_implicit_value_convertible< T, U >::value, int >::type = 0>
constexpr cpp::bitwizeshift::result< T, E >::result ( U &&  value) const
constexprnoexcept

Constructs a result object that contains a value.

The value is initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression value.

Note
This constructor is constexpr if the constructor of T selected by direct-initialization is constexpr
This constructor does not participate in overload resolution unless std::is_constructible_v<T, U&&> is true and decay_t<U> is neither in_place_t, in_place_error_t, nor a result type.
This constructor is explicit if and only if std::is_convertible_v<U&&, T> is false

Examples

Basic Usage:

auto get_value() -> cpp::result<std::string,int> {
return "hello world!"; // implicit conversion
}
Parameters
valuethe value to copy

Member Function Documentation

◆ and_then()

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

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);
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 T , typename E >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, 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:

assert(r.error() == 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 T , typename E >
constexpr RESULT_WARN_UNUSED auto cpp::bitwizeshift::result< T, E >::error ( ) const && -> E
constexprnoexcept

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:

assert(r.error() == 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 T , typename E >
template<typename U >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, 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));
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 T , typename E >
template<typename U >
constexpr RESULT_WARN_UNUSED auto cpp::bitwizeshift::result< T, E >::error_or ( U &&  default_error) const & -> error_type
constexpr

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));
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/4]

template<typename T , 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< T, E >::expect ( String &&  message) && -> typename std::add_rvalue_reference< T >::type

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:

assert(r.error() == 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

◆ expect() [2/4]

template<typename T , 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< T, E >::expect ( String &&  message) & -> typename std::add_lvalue_reference< T >::type

}

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
Returns
the value of *this

◆ expect() [3/4]

template<typename T , 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< T, E >::expect ( String &&  message) const && -> typename std::add_rvalue_reference< typename std::add_const< T >::type >::type

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:

assert(r.error() == 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

◆ expect() [4/4]

template<typename T , 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< T, E >::expect ( String &&  message) const & -> typename std::add_lvalue_reference< typename std::add_const< T >::type >::type

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:

assert(r.error() == 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

◆ flat_map() [1/2]

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

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

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 to_string(int) -> cpp::result<std::string,int>;
auto r = cpp::result<int,int>{42};
assert(r.flat_map(to_string) == "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() [2/2]

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

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

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 to_string(int) -> cpp::result<std::string,int>;
auto r = cpp::result<int,int>{42};
assert(r.flat_map(to_string) == "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() [1/2]

template<typename T , typename E >
template<typename Fn >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, 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);
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 T , typename E >
template<typename Fn >
constexpr RESULT_WARN_UNUSED auto cpp::bitwizeshift::result< T, E >::flat_map_error ( Fn &&  fn) const & -> detail::invoke_result_t< Fn, const E & >
constexpr

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);
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 T , typename E >
constexpr RESULT_WARN_UNUSED auto cpp::bitwizeshift::result< T, E >::has_error ( ) const -> bool
constexprnoexcept

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>{cpp::fail(42)}.has_error());
Returns
true if *this contains an error, false if *this contains a value

◆ has_value()

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

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>{cpp::fail(42)}.has_value());
Returns
true if *this contains a value, false if *this contains an error

◆ map() [1/2]

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

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

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 to_string(int) -> std::string;
auto r = cpp::result<int,int>{42};
assert(r.map(to_string) == "42");
assert(r.map(to_string) == cpp::fail(42));
Parameters
fnthe function to invoke with this
Returns
The result result of the function invoked

◆ map() [2/2]

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

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

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 to_string(int) -> std::string;
auto r = cpp::result<int,int>{42};
assert(r.map(to_string) == "42");
assert(r.map(to_string) == cpp::fail(42));
Parameters
fnthe function to invoke with this
Returns
The result result of the function invoked

◆ map_error() [1/2]

template<typename T , typename E >
template<typename Fn >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::map_error ( Fn &&  fn) && -> result< T, 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);
assert(r.map_error(to_string) == cpp::fail("42"));
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 T , typename E >
template<typename Fn >
constexpr RESULT_WARN_UNUSED auto cpp::bitwizeshift::result< T, E >::map_error ( Fn &&  fn) const & -> result< T, detail::invoke_result_t< Fn, const E & >>
constexpr

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);
assert(r.map_error(to_string) == cpp::fail("42"));
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 T , typename E >
constexpr RESULT_WARN_UNUSED cpp::bitwizeshift::result< T, E >::operator bool ( ) const
explicitconstexprnoexcept

Contextually convertible to true if *this contains a value.

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

Examples

Basic Usage:

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

◆ operator*() [1/4]

template<typename T , typename E >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::operator* ( ) && -> typename std::add_rvalue_reference< T >::type
noexcept

Retrieves a reference to the contained value.

This operator exists to give result an optional-like API for cases where it's known that the result already contains a value.

Care must be taken to ensure that this is only used in safe contexts where a T value is active.

Note
The behaviour is undefined if *this does not contain a value

Examples

Basic Usage:

make_widget()
};
(*r).do_something();
consume(*r);
Returns
a reference to the contained value

◆ operator*() [2/4]

template<typename T , typename E >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::operator* ( ) & -> typename std::add_lvalue_reference< T >::type
noexcept

Retrieves a reference to the contained value.

This operator exists to give result an optional-like API for cases where it's known that the result already contains a value.

Care must be taken to ensure that this is only used in safe contexts where a T value is active.

Note
The behaviour is undefined if *this does not contain a value

Examples

Basic Usage:

make_widget()
};
(*r).do_something();
consume(*r);
Returns
a reference to the contained value

◆ operator*() [3/4]

template<typename T , typename E >
constexpr RESULT_WARN_UNUSED auto cpp::bitwizeshift::result< T, E >::operator* ( ) const && -> typename std::add_rvalue_reference< typename std::add_const< T >::type >::type
constexprnoexcept

Retrieves a reference to the contained value.

This operator exists to give result an optional-like API for cases where it's known that the result already contains a value.

Care must be taken to ensure that this is only used in safe contexts where a T value is active.

Note
The behaviour is undefined if *this does not contain a value

Examples

Basic Usage:

make_widget()
};
(*r).do_something();
consume(*r);
Returns
a reference to the contained value

◆ operator*() [4/4]

template<typename T , typename E >
constexpr RESULT_WARN_UNUSED auto cpp::bitwizeshift::result< T, E >::operator* ( ) const & -> typename std::add_lvalue_reference< typename std::add_const< T >::type >::type
constexprnoexcept

Retrieves a reference to the contained value.

This operator exists to give result an optional-like API for cases where it's known that the result already contains a value.

Care must be taken to ensure that this is only used in safe contexts where a T value is active.

Note
The behaviour is undefined if *this does not contain a value

Examples

Basic Usage:

make_widget()
};
(*r).do_something();
consume(*r);
Returns
a reference to the contained value

◆ operator->() [1/2]

template<typename T , typename E >
constexpr RESULT_WARN_UNUSED auto cpp::bitwizeshift::result< T, E >::operator-> ( ) const -> typename std::remove_reference< typename std::add_const< T >::type >::type *
constexprnoexcept

Retrieves a pointer to the contained value.

This operator exists to give result an optional-like API for cases where it's known that the result already contains a value.

Care must be taken to ensure that this is only used in safe contexts where a T value is active.

Note
The behavior is undefined if *this does not contain a value.

Examples

Basic Usage:

make_widget()
};
r->do_something();
Returns
a pointer to the contained value

◆ operator->() [2/2]

template<typename T , typename E >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::operator-> ( ) -> typename std::remove_reference< T >::type *
noexcept

Retrieves a pointer to the contained value.

This operator exists to give result an optional-like API for cases where it's known that the result already contains a value.

Care must be taken to ensure that this is only used in safe contexts where a T value is active.

Note
The behavior is undefined if *this does not contain a value.

Examples

Basic Usage:

make_widget()
};
r->do_something();
Returns
a pointer to the contained value

◆ operator=() [1/7]

template<typename T , typename E >
template<typename E2 , typename = typename std::enable_if<detail::result_is_failure_assignable<E,const E2&>::value>::type>
auto cpp::bitwizeshift::result< T, 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=() [2/7]

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

Copy assigns the result stored in other.

Note
This assignment operator only participates in overload resolution if the following conditions are met:
  • std::is_nothrow_copy_constructible_v<T> is true, and
  • std::is_nothrow_copy_constructible_v<E> is true this restriction guarantees that no '
This assignment operator is defined as trivial if the following conditions are all true:
  • std::is_trivially_copy_constructible<T>::value
  • std::is_trivially_copy_constructible<E>::value
  • std::is_trivially_copy_assignable<T>::value
  • std::is_trivially_copy_assignable<E>::value
  • std::is_trivially_destructible<T>::value
  • std::is_trivially_destructible<E>::value
Parameters
otherthe other result to copy

◆ operator=() [3/7]

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

Copy-converts the state of other.

If both *this and other contain either values or errors, the underlying value is constructed as if through assignment.

Otherwise if *this contains a value, but other contains an error, then the contained value is destroyed by calling its destructor. *this will no longer contain a value after the call, and will now contain E constructed as if direct-initializing (but not direct-list-initializing) an object with an argument of const E2&.

If other contains a value and *this contains an error, then the contained error is destroyed by calling its destructor. *this now contains a value constructed as if direct-initializing (but not direct-list-initializing) an object with an argument of const T2&.

Note
The function does not participate in overload resolution unless
  • std::is_nothrow_constructible_v<T, const T2&>, std::is_assignable_v<T&, const T2&>, std::is_nothrow_constructible_v<E, const E2&>, std::is_assignable_v<E&, const E2&> are all true.
  • T is not constructible, convertible, or assignable from any expression of type (possibly const) result<T2,E2>
Parameters
otherthe other result object to convert
Returns
reference to (*this)

◆ operator=() [4/7]

template<typename T , typename E >
template<typename E2 , typename = typename std::enable_if<detail::result_is_failure_assignable<E,E2&&>::value>::type>
auto cpp::bitwizeshift::result< T, 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)

◆ operator=() [5/7]

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

Move assigns the result stored in other.

Note
This assignment operator only participates in overload resolution if the following conditions are met:
  • std::is_nothrow_copy_constructible_v<T> is true, and
  • 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<T>::value
  • std::is_trivially_move_constructible<E>::value
  • std::is_trivially_move_assignable<T>::value
  • std::is_trivially_move_assignable<E>::value
  • std::is_trivially_destructible<T>::value
  • std::is_trivially_destructible<E>::value
Parameters
otherthe other result to copy

◆ operator=() [6/7]

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

Move-converts the state of other.

If both *this and other contain either values or errors, the underlying value is constructed as if through move-assignment.

Otherwise if *this contains a value, but other contains an error, then the contained value is destroyed by calling its destructor. *this will no longer contain a value after the call, and will now contain E constructed as if direct-initializing (but not direct-list-initializing) an object with an argument of E2&&.

If other contains a value and *this contains an error, then the contained error is destroyed by calling its destructor. *this now contains a value constructed as if direct-initializing (but not direct-list-initializing) an object with an argument of T2&&.

Note
The function does not participate in overload resolution unless
  • std::is_nothrow_constructible_v<T, T2&&>, std::is_assignable_v<T&, T2&&>, std::is_nothrow_constructible_v<E, E2&&>, std::is_assignable_v<E&, E2&&> are all true.
  • T is not constructible, convertible, or assignable from any expression of type (possibly const) result<T2,E2>
Parameters
otherthe other result object to convert
Returns
reference to (*this)

◆ operator=() [7/7]

template<typename T , typename E >
template<typename U , typename = typename std::enable_if<detail::result_is_value_assignable<T,U>::value>::type>
auto cpp::bitwizeshift::result< T, E >::operator= ( U &&  value) -> result &
noexcept

Perfect-forwarded assignment.

Depending on whether *this contains a value before the call, the contained value is either direct-initialized from std::forward<U>(value) or assigned from std::forward<U>(value).

Note
The function does not participate in overload resolution unless
  • std::decay_t<U> is not a result type,
  • std::decay_t<U> is not a failure type
  • std::is_nothrow_constructible_v<T, U> is true
  • std::is_assignable_v<T&, U> is true
  • and at least one of the following is true:
    • T is not a scalar type;
    • std::decay_t<U> is not T.
Parameters
valueto assign to the contained value
Returns
reference to (*this)

◆ value() [1/4]

template<typename T , typename E >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::value ( ) && -> typename std::add_rvalue_reference< T >::type

Returns a reference to the contained value.

This function provides checked (throwing) access to the underlying value. The constness and refness of this result is propagated to the underlying reference.

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:

assert(cpp::result<int,int>{42}.value() == 42);
std::make_unique<int>(42)
};
auto s = std::move(r).value();
try {
auto v = r.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.
Returns
the value of *this

◆ value() [2/4]

template<typename T , typename E >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::value ( ) & -> typename std::add_lvalue_reference< T >::type

Returns a reference to the contained value.

This function provides checked (throwing) access to the underlying value. The constness and refness of this result is propagated to the underlying reference.

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:

assert(cpp::result<int,int>{42}.value() == 42);
std::make_unique<int>(42)
};
auto s = std::move(r).value();
try {
auto v = r.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.
Returns
the value of *this

◆ value() [3/4]

template<typename T , typename E >
constexpr RESULT_WARN_UNUSED auto cpp::bitwizeshift::result< T, E >::value ( ) const && -> typename std::add_rvalue_reference< typename std::add_const< T >::type >::type
constexpr

Returns a reference to the contained value.

This function provides checked (throwing) access to the underlying value. The constness and refness of this result is propagated to the underlying reference.

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:

assert(cpp::result<int,int>{42}.value() == 42);
std::make_unique<int>(42)
};
auto s = std::move(r).value();
try {
auto v = r.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.
Returns
the value of *this

◆ value() [4/4]

template<typename T , typename E >
constexpr RESULT_WARN_UNUSED auto cpp::bitwizeshift::result< T, E >::value ( ) const & -> typename std::add_lvalue_reference< typename std::add_const< T >::type >::type
constexpr

Returns a reference to the contained value.

This function provides checked (throwing) access to the underlying value. The constness and refness of this result is propagated to the underlying reference.

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:

assert(cpp::result<int,int>{42}.value() == 42);
std::make_unique<int>(42)
};
auto s = std::move(r).value();
try {
auto v = r.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.
Returns
the value of *this

◆ value_or() [1/2]

template<typename T , typename E >
template<typename U >
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto cpp::bitwizeshift::result< T, E >::value_or ( U &&  default_value) && -> typename std::remove_reference< T >::type

Returns the contained value if *this has a value, otherwise returns default_value.

Examples

Basic Usage:

auto r = cpp::result<int,int>{42};
assert(r.value_or(0) == 42);
assert(r.value_or(0) == 0);
Parameters
default_valuethe value to use in case *this contains an error
Returns
the contained value or default_value

◆ value_or() [2/2]

template<typename T , typename E >
template<typename U >
constexpr RESULT_WARN_UNUSED auto cpp::bitwizeshift::result< T, E >::value_or ( U &&  default_value) const & -> typename std::remove_reference< T >::type
constexpr

Returns the contained value if *this has a value, otherwise returns default_value.

Examples

Basic Usage:

auto r = cpp::result<int,int>{42};
assert(r.value_or(0) == 42);
assert(r.value_or(0) == 0);
Parameters
default_valuethe value to use in case *this contains an error
Returns
the contained value or default_value

The documentation for this class was generated from the following file:
cpp::bitwizeshift::result
The class template result manages result results from APIs, while encoding possible failure condition...
Definition: result.hpp:309
cpp::bitwizeshift::result::has_error
constexpr RESULT_WARN_UNUSED auto has_error() const noexcept -> bool
Returns true if *this contains an error.
cpp::bitwizeshift::fail
constexpr RESULT_WARN_UNUSED auto fail(E &&e) noexcept(std::is_nothrow_constructible< typename std::decay< E >::type, E >::value) -> failure< typename std::decay< E >::type >
Deduces and constructs a failure type from e.
cpp::bitwizeshift::fail
constexpr RESULT_WARN_UNUSED auto fail(std::initializer_list< U > ilist, Args &&...args) noexcept(std::is_nothrow_constructible< E, std::initializer_list< U >, Args... >::value) -> failure< E >
Constructs a failure type from an initializer list and series of arguments.
cpp::bitwizeshift::result::map
constexpr RESULT_WARN_UNUSED auto map(Fn &&fn) const &-> result< detail::invoke_result_t< Fn, const T & >, E >
Invokes the function fn with the value of this result as the argument.
cpp::bitwizeshift::result::error
constexpr RESULT_WARN_UNUSED 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.
cpp::bitwizeshift::bad_result_access
An exception thrown when result::value is accessed without a contained value.
Definition: result.hpp:312
cpp::bitwizeshift::result::value_or
constexpr RESULT_WARN_UNUSED auto value_or(U &&default_value) const &-> typename std::remove_reference< T >::type
Returns the contained value if *this has a value, otherwise returns default_value.
cpp::bitwizeshift::result::and_then
constexpr RESULT_WARN_UNUSED 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 contain...
cpp::bitwizeshift::result::has_value
constexpr RESULT_WARN_UNUSED auto has_value() const noexcept -> bool
Returns true if *this contains a value.
cpp::bitwizeshift::bad_result_access::error
auto error() &noexcept -> E &
Gets the underlying error.
cpp::bitwizeshift::result::value
RESULT_WARN_UNUSED RESULT_CPP14_CONSTEXPR auto value() &-> typename std::add_lvalue_reference< T >::type
Returns a reference to the contained value.
cpp::bitwizeshift::result::flat_map
constexpr RESULT_WARN_UNUSED auto flat_map(Fn &&fn) const &-> detail::invoke_result_t< Fn, const T & >
Invokes the function fn with the value of this result as the argument.
cpp::bitwizeshift::result::map_error
constexpr RESULT_WARN_UNUSED auto map_error(Fn &&fn) const &-> result< T, detail::invoke_result_t< Fn, const E & >>
Invokes the function fn with the error of this result as the argument.
cpp::bitwizeshift::result::error_or
constexpr RESULT_WARN_UNUSED auto error_or(U &&default_error) const &-> error_type
Returns the contained error if *this has an error, otherwise returns default_error.
cpp::bitwizeshift::result::expect
RESULT_CPP14_CONSTEXPR auto expect(String &&message) &-> typename std::add_lvalue_reference< T >::type
}