Skip to main content

Result

#include <result.hpp>
Result type representing either success (Ok) or failure (Err) Example usage:
Result<int> parse_port(const std::string& s) {
    if (s.empty()) {
        return [Result<int>::Err](#err)(400, "Port cannot be empty");
    }
    int port = std::stoi(s);
    if (port < 1 || port > 65535) {
        return [Result<int>::Err](#err)(400, "Port must be in range [1, 65535]");
    }
    return [Result<int>::Ok](#ok)(port);
}

auto result = parse_port("8080");
if (result.is_ok()) {
    int port = result.unwrap();
    // Use port...
} else {
    spdlog::error("Parse failed: {} (code={})", result.error(), result.code());
}

Public Methods

ReturnNameDescription
ResultDeleted constructor.
Result
Result
boolis_ok constCheck if result is successful.
boolis_err constCheck if result is an error.
const T &unwrap constGet the value (throws if error) Use is_ok() to check first, or use unwrap_or()
T &unwrap
Tunwrap_or constGet the value or a default.
Tunwrap_or_else constGet the value or compute from error.
const std::string &error constGet error message.
uint32_tcode constGet error code.
automap constMap the value if Ok, preserve error if Err.
operator bool const explicitConvert to bool (true if Ok)

Result

Result() = delete
Deleted constructor.

Result

inline
inline Result(const Result & other)

Result

inline
inline Result(Result && other) noexcept

is_ok

const
inline bool is_ok() const noexcept
Check if result is successful.

is_err

const
inline bool is_err() const noexcept
Check if result is an error.

unwrap

const
inline const T & unwrap() const
Get the value (throws if error) Use is_ok() to check first, or use unwrap_or()

unwrap

inline
inline T & unwrap()

unwrap_or

const
inline T unwrap_or(const T & default_value) const
Get the value or a default.

unwrap_or_else

const
template<typename F> inline T unwrap_or_else(F && op) const
Get the value or compute from error.

error

const
inline const std::string & error() const noexcept
Get error message.

code

const
inline uint32_t code() const noexcept
Get error code.

map

const
template<typename F> inline auto map(F && func) const
Map the value if Ok, preserve error if Err.

operator bool

const explicit
inline explicit operator bool() const noexcept
Convert to bool (true if Ok)

Public Static Methods

ReturnNameDescription
ResultOk staticCreate a successful result.
ResultOk static
ResultErr staticCreate an error result.

Ok

static
static inline Result Ok(const T & value)
Create a successful result.

Ok

static
static inline Result Ok(T && value)

Err

static
static inline Result Err(uint32_t code, std::string_view msg)
Create an error result.

Parameters

  • code Error code (use HTTP-style: 400=bad request, 404=not found, 500=internal error)
  • msg Human-readable error message

Private Attributes

ReturnNameDescription
Tvalue_
boolis_ok_
std::stringerror_msg_
uint32_terror_code_

value_

T value_

is_ok_

bool is_ok_

error_msg_

std::string error_msg_

error_code_

uint32_t error_code_

Private Methods

ReturnNameDescription
Result
Result

Result

inline
inline Result(const T & value, bool ok, std::string msg, uint32_t code)

Result

inline
inline Result(T && value, bool ok, std::string msg, uint32_t code)