English | 简体中文
ccc::expected<T, E, Criterion> is a C++14 implementation and extension of std::expected. It not only provides "expected value" and "error value" semantics, but also supports custom Criterion types to extend "expected value with additional criterion information" semantics.
For example, when a string processing buffer is not large enough and data is truncated, there is a "value", but custom criterion information can be attached to indicate "the string has a value but was truncated".
When ignoring the third template parameter, the default default_criterion is used, and its behavior is similar to a standard std::expected.
- C++14 compatible implementation of expected/monadic error handling.
- Optional third template parameter
Criterionfor extended semantics. - Monadic operations (requires C++17 and above):
and_then,or_else,transform,transform_error. - Basic compatibility with
std::expectedsemantics when using the default criterion. - Works with references, pointers, value types and many scenarios.
-
Include the header:
#include "ccc/expected.hh"or, when using C++ modules:
import ccc.expected;
ccc::expected<int, std::string> result = 42;
if (result.has_value()) {
int value = *result;
// use value
}// Custom criterion for truncated string
struct truncate_criterion {
using value_type = bool;
static constexpr value_type default_error_value = false;
bool was_truncated = false;
constexpr bool has_value() const noexcept { return true; }
constexpr bool operator==(const truncate_criterion& other) const noexcept {
return was_truncated == other.was_truncated;
}
};
static_assert(ccc::is_criterion_v<truncate_criterion>, "must satisfy criterion requirements");
// Use with expected
ccc::expected<std::string, std::string, truncate_criterion> process_string() {
// ... process and detect truncation
return ccc::expected<std::string, std::string, truncate_criterion>(
std::move(result), truncate_criterion{.was_truncated = true});
}ccc::expected<T, E, Criterion = default_criterion>: The main expected typeccc::unexpected<E>: Wrapper for error valuesccc::bad_expected_access<E>: Exception thrown when accessing value of unexpectedccc::default_criterion: Default criterion with simple boolean semantics
All monadic operations are available in C++17 and later:
and_then: Chain operations that return expectedor_else: Handle errors by returning expectedtransform: Transform the valuetransform_error: Transform the error
-
Criterion types must satisfy specific requirements:
- Have a nested
value_typetypedef - Implement
has_value()method (noexcept) - Provide
default_error_valuestatic member - Default constructible, and constructible via the
default_error_valuestatic member
- Have a nested
-
For complete usage examples, see:
test/expected_test/expected_test.cc