Loading...
Searching...
No Matches

A sum type representing either a success value or an error. More...

#include <containers.hpp>

Public Member Functions

const E & error () const
 Unwrap the error.
 
bool is_err () const
 Check if this result is an error.
 
bool is_ok () const
 Check if this result is a success.
 
const T & value () const
 Unwrap the success value.
 

Static Public Member Functions

static Result err (E error)
 Create an error result.
 
static Result ok (T value)
 Create a success result.
 

Detailed Description

template<typename T, typename E = std::string>
class containers::Result< T, E >

A sum type representing either a success value or an error.

Inspired by Rust's Result<T, E>. Uses std::variant internally.

Template Parameters
TThe success type.
EThe error type (default: std::string).

Example:

Result<int, std::string> divide(int a, int b) {
if (b == 0) return Result<int, std::string>::err("division by zero");
}
static Result ok(T value)
Create a success result.
static Result err(E error)
Create an error result.
See also
Stack, RingBuffer

Definition at line 220 of file containers.hpp.

Member Function Documentation

◆ err()

template<typename T, typename E = std::string>
static Result containers::Result< T, E >::err ( E error)
inlinestatic

Create an error result.

Parameters
errorThe error value.
Returns
A Result containing the error.

Definition at line 234 of file containers.hpp.

234{ return Result(ErrTag{}, std::move(error)); }
A sum type representing either a success value or an error.
const E & error() const
Unwrap the error.

References error().

◆ error()

template<typename T, typename E = std::string>
const E & containers::Result< T, E >::error ( ) const
inlinenodiscard

Unwrap the error.

Returns
The contained error.
Exceptions
std::bad_variant_accessif this is a success.

Definition at line 256 of file containers.hpp.

256{ return std::get<E>(data_); }

Referenced by err().

◆ is_err()

template<typename T, typename E = std::string>
bool containers::Result< T, E >::is_err ( ) const
inlinenodiscard

Check if this result is an error.

Definition at line 242 of file containers.hpp.

242{ return !is_ok(); }
bool is_ok() const
Check if this result is a success.

References is_ok().

◆ is_ok()

template<typename T, typename E = std::string>
bool containers::Result< T, E >::is_ok ( ) const
inlinenodiscard

Check if this result is a success.

Definition at line 237 of file containers.hpp.

237 {
238 return std::holds_alternative<T>(data_);
239 }

Referenced by is_err().

◆ ok()

template<typename T, typename E = std::string>
static Result containers::Result< T, E >::ok ( T value)
inlinestatic

Create a success result.

Parameters
valueThe success value.
Returns
A Result containing the value.

Definition at line 227 of file containers.hpp.

227{ return Result(std::move(value)); }
const T & value() const
Unwrap the success value.

References value().

◆ value()

template<typename T, typename E = std::string>
const T & containers::Result< T, E >::value ( ) const
inlinenodiscard

Unwrap the success value.

Returns
The contained value.
Exceptions
std::bad_variant_accessif this is an error.

Definition at line 249 of file containers.hpp.

249{ return std::get<T>(data_); }

Referenced by ok().


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