expected_ec and make_unexpected_ec
simple_enum
Overview
expected_ecis a template alias simplifying usage ofstd::expectedwithstd::error_codefor error handling.make_unexpected_ecis a utility function creating anunexpectedobject initialized with astd::error_codegenerated from an enumeration representing error conditions.
Usage
Ensure inclusion .
#include <simple_enum/generic_error_category.hpp>
expected_ec Type Alias
template<typename T>
using expected_ec = std::expected<T, std::error_code>;
- Usage: Represents a type that may contain a value of type
Tor an error of typestd::error_code.
unexpected_ec Type Alias
using unexpected_ec = std::unexpected<std::error_code>;
- Usage: Simplifies the declaration of an
unexpectedtype carrying astd::error_code.
make_unexpected_ec Function
template<concepts::error_enum ErrorEnum>
[[nodiscard]] auto make_unexpected_ec(ErrorEnum e) -> unexpected_ec;
- Usage: Creates an
unexpected_ecobject from an enumeration valueerepresenting an error. This leveragesmake_error_codeto convert enumeration tostd::error_code.
Example Usage
enum class error_code { none, error1, error2, error3 };
// Using expected_ec and make_unexpected_ec for error handling
auto func() -> expected_ec<int> {
if(/* error condition */) {
return make_unexpected_ec(error_code::error1);
}
return 42; // Success path returns a value
}