My Project
Public Types | Public Member Functions | Related Functions | List of all members
bsl::result< T, E > Class Template Referencefinal

Provides the ability to return T or E from a function, ensuring that T is only created if an error is not present. More...

#include <result.hpp>

Public Types

using type = T
 alias for: T
 

Public Member Functions

constexpr result (T const &t) noexcept(//PRQA S 2180//NOLINT is_nothrow_copy_constructible< T >::value)
 Constructs a bsl::result that contains T, by copying "t". More...
 
constexpr result (T &&t) noexcept(//PRQA S 2180//NOLINT is_nothrow_move_constructible< T >::value)
 Constructs a bsl::result that contains T, by moving "t". More...
 
template<typename... ARGS>
constexpr result (bsl::in_place_t const &ip, ARGS &&... args) noexcept(is_nothrow_constructible< T, ARGS... >::value)
 Constructs a bsl::result that contains T by constructing T in place. More...
 
constexpr result (E const &e, sloc_type const &sloc=here()) noexcept
 Constructs a bsl::result that contains E, by copying "e". More...
 
constexpr result (E &&e, sloc_type const &sloc=here()) noexcept
 Constructs a bsl::result that contains E, by moving "e". More...
 
BSL_CONSTEXPR ~result () noexcept(is_nothrow_destructible< T >::value)
 Destroyes a previously created bsl::result. Since we require E to be trivially destructible, we only need to call a destructor if this object contains a T. More...
 
constexpr result (result const &o) noexcept(//PRQA S 4285 is_nothrow_copy_constructible< T >::value)
 copy constructor More...
 
constexpr result (result &&o) noexcept(//PRQA S 4285 is_nothrow_move_constructible< T >::value)
 move constructor More...
 
constexpr resultoperator= (result const &o) &noexcept(is_nothrow_copy_constructible< T >::value &&is_nothrow_swappable< T >::value)
 copy assignment More...
 
constexpr resultoperator= (result &&o) &noexcept(is_nothrow_move_constructible< T >::value &&is_nothrow_swappable< T >::value)
 move assignment More...
 
template<typename O >
constexpr result (O val) noexcept=delete
 This constructor allows for single argument constructors without the need to mark them as explicit as it will absorb any incoming potential implicit conversion and prevent it. More...
 
constexpr T * get_if () &noexcept
 Returns a handle to T if this object contains T, otherwise it returns a nullptr. More...
 
constexpr T * get_if () &&noexcept=delete
 Prevents the use of get_if() on temporary objects, which would result in UB. More...
 
constexpr T const * get_if () const &noexcept
 Returns a handle to T if this object contains T, otherwise it returns a nullptr. More...
 
constexpr T const * get_if () const &&noexcept=delete
 Prevents the use of get_if() on temporary objects, which would result in UB. More...
 
constexpr E errc (E const &fallback=E{}) const noexcept
 Returns an error code if this object contains E, otherwise it returns "fallback". More...
 
constexpr bool success () const noexcept
 Returns true if the bsl::result contains T, otherwise, if the bsl::result contains an error code, returns false. More...
 
constexpr bool failure () const noexcept
 Returns true if the bsl::result contains E, otherwise, if the bsl::result contains T, returns false. More...
 

Related Functions

(Note that these are not member functions.)

template<typename T , typename E >
constexpr bool operator== (result< T, E > const &lhs, result< T, E > const &rhs) noexcept
 Returns true if the lhs is equal to the rhs, false otherwise. More...
 
template<typename T , typename E >
constexpr bool operator!= (result< T, E > const &lhs, result< T, E > const &rhs) noexcept
 Returns false if the lhs is equal to the rhs, true otherwise. More...
 

Detailed Description

template<typename T, typename E = errc_type>
class bsl::result< T, E >

Provides the ability to return T or E from a function, ensuring that T is only created if an error is not present.

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_overview() noexcept
{
if (auto const *const ptr = res.get_if()) {
bsl::print() << "success: " << *ptr << bsl::endl;
}
}
}
Template Parameters
Tthe nullable type
Ethe error type to use

Constructor & Destructor Documentation

◆ result() [1/8]

template<typename T, typename E = errc_type>
constexpr bsl::result< T, E >::result ( T const &  t)
inlinenoexcept

Constructs a bsl::result that contains T, by copying "t".

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_t_copy_constructor() noexcept
{
bool const val{true};
if (auto const *const ptr = res.get_if()) {
bsl::print() << "success: " << *ptr << bsl::endl;
}
}
}

SUPPRESSION: PRQA 2180 - exception required

  • We suppress this because A12-1-4 states that all constructors that are callable from a fundamental type should be marked as explicit. This is a fundamental type, but all implicit conversions are disabled through the use of the implicit general template constructor that is deleted which absorbs all incoming potential implicit conversions.
Parameters
tthe value being copied
Exceptions
throwsif T's copy constructor throws

◆ result() [2/8]

template<typename T, typename E = errc_type>
constexpr bsl::result< T, E >::result ( T &&  t)
inlinenoexcept

Constructs a bsl::result that contains T, by moving "t".

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_t_move_constructor() noexcept
{
bool val{true};
bsl::result<bool> const res{bsl::move(val)};
if (auto const *const ptr = res.get_if()) {
bsl::print() << "success: " << *ptr << bsl::endl;
}
}
}

SUPPRESSION: PRQA 2180 - exception required

  • We suppress this because A12-1-4 states that all constructors that are callable from a fundamental type should be marked as explicit. This is a fundamental type, but all implicit conversions are disabled through the use of the implicit general template constructor that is deleted which absorbs all incoming potential implicit conversions.
Parameters
tthe value being moved
Exceptions
throwsif T's move constructor throws

◆ result() [3/8]

template<typename T, typename E = errc_type>
template<typename... ARGS>
constexpr bsl::result< T, E >::result ( bsl::in_place_t const &  ip,
ARGS &&...  args 
)
inlinenoexcept

Constructs a bsl::result that contains T by constructing T in place.

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_t_in_place_constructor() noexcept
{
bsl::result<bool> const res{bsl::in_place, true};
if (auto const *const ptr = res.get_if()) {
bsl::print() << "success: " << *ptr << bsl::endl;
}
}
}
Parameters
ipprovide bsl::in_place to construct in place
argsthe arguments to create T with
Exceptions
throwsif T's constructor throws

◆ result() [4/8]

template<typename T, typename E = errc_type>
constexpr bsl::result< T, E >::result ( E const &  e,
sloc_type const &  sloc = here() 
)
inlinenoexcept

Constructs a bsl::result that contains E, by copying "e".

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_errc_copy_constructor() noexcept
{
if (res.errc() == bsl::errc_failure) {
bsl::print() << "success\n";
}
}
}

SUPPRESSION: PRQA 2180 - false positive

  • We suppress this because A12-1-4 states that all constructors that are callable from a fundamental type should be marked as explicit. This is not a fundamental type and there for does not apply.
Parameters
ethe error code being copied
slocthe source location of the error

◆ result() [5/8]

template<typename T, typename E = errc_type>
constexpr bsl::result< T, E >::result ( E &&  e,
sloc_type const &  sloc = here() 
)
inlinenoexcept

Constructs a bsl::result that contains E, by moving "e".

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_errc_move_constructor() noexcept
{
constexpr bsl::int32 val{42};
bsl::errc_type my_errc1{val};
constexpr bsl::errc_type my_errc2{val};
bsl::result<bool> const res{bsl::move(my_errc1)};
if (res.errc() == my_errc2) {
bsl::print() << "success\n";
}
}
}

SUPPRESSION: PRQA 2180 - false positive

  • We suppress this because A12-1-4 states that all constructors that are callable from a fundamental type should be marked as explicit. This is not a fundamental type and there for does not apply.
Parameters
ethe error code being moved
slocthe source location of the error

◆ ~result()

template<typename T, typename E = errc_type>
BSL_CONSTEXPR bsl::result< T, E >::~result ( )
inlinenoexcept

Destroyes a previously created bsl::result. Since we require E to be trivially destructible, we only need to call a destructor if this object contains a T.

Exceptions
throwsif the bsl::result stores a T and T's destructor throws

◆ result() [6/8]

template<typename T, typename E = errc_type>
constexpr bsl::result< T, E >::result ( result< T, E > const &  o)
inlinenoexcept

copy constructor

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_copy_constructor() noexcept
{
bsl::result<bool> const res1{bsl::in_place, true};
bsl::result<bool> const res2{res1}; // NOLINT
if (auto const *const ptr = res2.get_if()) {
bsl::print() << "success: " << *ptr << bsl::endl;
}
}
}

SUPPRESSION: PRQA 4285 - false positive

  • We suppress this because A12-8-1 states a copy/move should not have a side effect other than the copy/move itself. This is a false positive because there are not side effects in this code below. PRQA is not properly handling the union as allowed by AUTOSAR.

SUPPRESSION: PRQA 4050 - false positive

  • We suppress this because A12-1-1 states that all member variables should be explicitly initialized. It does not state that they must be in the initializer list. Furthermore, it is impossible to initialize union members in an initializer list in a copy/move constructor, which PRQA should be capable of detecting, and it doesn't.
Parameters
othe object being copied
Exceptions
throwsif the bsl::result stores a T and T's copy constructor throws

◆ result() [7/8]

template<typename T, typename E = errc_type>
constexpr bsl::result< T, E >::result ( result< T, E > &&  o)
inlinenoexcept

move constructor

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_move_constructor() noexcept
{
bsl::result<bool> const res2{bsl::move(res1)};
if (auto const *const ptr = res2.get_if()) {
bsl::print() << "success: " << *ptr << bsl::endl;
}
}
}

SUPPRESSION: PRQA 4285 - false positive

  • We suppress this because A12-8-1 states a copy/move should not have a side effect other than the copy/move itself. This is a false positive because the only side effect is the copy/move as required. PRQA is not properly handling the union as allows by AUTOSAR.

SUPPRESSION: PRQA 4050 - false positive

  • We suppress this because A12-1-1 states that all member variables should be explicitly initialized. It does not state that they must be in the initializer list. Furthermore, it is impossible to initialize union members in an initializer list in a copy/move constructor, which PRQA should be capable of detecting, and it doesn't.
Parameters
othe object being moved
Exceptions
throwsif the bsl::result stores a T and T's move constructor throws

◆ result() [8/8]

template<typename T, typename E = errc_type>
template<typename O >
constexpr bsl::result< T, E >::result ( val)
deletenoexcept

This constructor allows for single argument constructors without the need to mark them as explicit as it will absorb any incoming potential implicit conversion and prevent it.

SUPPRESSION: PRQA 2180 - false positive

  • We suppress this because A12-1-4 states that all constructors that are callable from a fundamental type should be marked as explicit. This is callable with a fundamental type, but it is marked as "delete" which means it does not apply.
Template Parameters
Othe type that could be implicitly converted
Parameters
valthe value that could be implicitly converted

Member Function Documentation

◆ operator=() [1/2]

template<typename T, typename E = errc_type>
constexpr result& bsl::result< T, E >::operator= ( result< T, E > const &  o) &
inlinenoexcept

copy assignment

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_copy_assignment() noexcept
{
bsl::result<bool> const res1{bsl::in_place, true};
res2 = res1;
if (auto const *const ptr = res2.get_if()) {
bsl::print() << "success: " << *ptr << bsl::endl;
}
}
}
Parameters
othe object being copied
Returns
a reference to *this
Exceptions
throwsif the bsl::result stores a T and T's copy constructor throws or swapping T throws

◆ operator=() [2/2]

template<typename T, typename E = errc_type>
constexpr result& bsl::result< T, E >::operator= ( result< T, E > &&  o) &
inlinenoexcept

move assignment

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_move_assignment() noexcept
{
res2 = bsl::move(res1);
if (auto const *const ptr = res2.get_if()) {
bsl::print() << "success: " << *ptr << bsl::endl;
}
}
}
Parameters
othe object being moved
Returns
a reference to *this
Exceptions
throwsif the bsl::result stores a T and T's move constructor throws or swapping T throws

◆ get_if() [1/4]

template<typename T, typename E = errc_type>
constexpr T* bsl::result< T, E >::get_if ( ) &
inlinenoexcept

Returns a handle to T if this object contains T, otherwise it returns a nullptr.

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_get_if() noexcept
{
bsl::result<bool> const res{bsl::in_place, true};
if (auto const *const ptr = res.get_if()) {
bsl::print() << "success: " << *ptr << bsl::endl;
}
}
}

SUPPRESSION: PRQA 4024 - false positive - non-automated

  • We suppress this because A9-3-1 states that a class should not return a non-const handle to an object. AUTOSAR provides an exception for classes that mimic a smart pointer or a container, which is what this class is doing. It should be noted that such exceptions are likely not detectable by PRQA, and thus, this suppression will likely always be required.
Returns
Returns a handle to T if this object contains T, otherwise it returns a nullptr.

◆ get_if() [2/4]

template<typename T, typename E = errc_type>
constexpr T* bsl::result< T, E >::get_if ( ) &&
deletenoexcept

Prevents the use of get_if() on temporary objects, which would result in UB.

Returns
Returns a handle to T if this object contains T, otherwise it returns a nullptr.

◆ get_if() [3/4]

template<typename T, typename E = errc_type>
constexpr T const* bsl::result< T, E >::get_if ( ) const &
inlinenoexcept

Returns a handle to T if this object contains T, otherwise it returns a nullptr.

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_get_if() noexcept
{
bsl::result<bool> const res{bsl::in_place, true};
if (auto const *const ptr = res.get_if()) {
bsl::print() << "success: " << *ptr << bsl::endl;
}
}
}
Returns
Returns a handle to T if this object contains T, otherwise it returns a nullptr.

◆ get_if() [4/4]

template<typename T, typename E = errc_type>
constexpr T const* bsl::result< T, E >::get_if ( ) const &&
deletenoexcept

Prevents the use of get_if() on temporary objects, which would result in UB.

Returns
Returns a handle to T if this object contains T, otherwise it returns a nullptr.

◆ errc()

template<typename T, typename E = errc_type>
constexpr E bsl::result< T, E >::errc ( E const &  fallback = E{}) const
inlinenoexcept

Returns an error code if this object contains E, otherwise it returns "fallback".

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_errc() noexcept
{
if (res.errc() == bsl::errc_failure) {
bsl::print() << "success\n";
}
}
}
Parameters
fallbackreturned if this bsl::result contains T
Returns
Returns an error code if this object contains E, otherwise it returns "or".

◆ success()

template<typename T, typename E = errc_type>
constexpr bool bsl::result< T, E >::success ( ) const
inlinenoexcept

Returns true if the bsl::result contains T, otherwise, if the bsl::result contains an error code, returns false.

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_success() noexcept
{
bsl::result<bool> const res{bsl::in_place, true};
if (res.success()) {
bsl::print() << "success\n";
}
}
}
Returns
Returns true if the bsl::result contains T, otherwise, if the bsl::result contains an error code, returns false.

◆ failure()

template<typename T, typename E = errc_type>
constexpr bool bsl::result< T, E >::failure ( ) const
inlinenoexcept

Returns true if the bsl::result contains E, otherwise, if the bsl::result contains T, returns false.

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_failure() noexcept
{
if (res.failure()) {
bsl::print() << "success\n";
}
}
}
Returns
Returns true if the bsl::result contains E, otherwise, if the bsl::result contains T, returns false.

Friends And Related Function Documentation

◆ operator==()

template<typename T , typename E >
constexpr bool operator== ( result< T, E > const &  lhs,
result< T, E > const &  rhs 
)
related

Returns true if the lhs is equal to the rhs, false otherwise.

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_equals() noexcept
{
bsl::result<bool> const res1{bsl::in_place, true};
bsl::result<bool> const res2{bsl::in_place, true};
if (res1 == res2) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the operator
rhsthe right hand side of the operator
Returns
Returns true if the lhs is equal to the rhs, false otherwise

◆ operator!=()

template<typename T , typename E >
constexpr bool operator!= ( result< T, E > const &  lhs,
result< T, E > const &  rhs 
)
related

Returns false if the lhs is equal to the rhs, true otherwise.

#include <bsl/result.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_result_not_equals() noexcept
{
bsl::result<bool> const res1{bsl::in_place, true};
bsl::result<bool> const res2{bsl::in_place, false};
if (res1 != res2) {
bsl::print() << "success\n";
}
if (res1 != res3) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the operator
rhsthe right hand side of the operator
Returns
Returns false if the lhs is equal to the rhs, true otherwise

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