Learn how to implement std::expected-like types in C++, enabling better error handling and optional values with a more expressive interface than traditional methods.
std::expected, C++, error handling, optional values, C++20, programming, software development
#include <iostream>
#include <variant> // For std::variant
#include <optional> // For std::optional
#include <string>
// Define a simple Expected type
template <typename T, typename E>
class Expected {
public:
Expected(T value) : data_(std::move(value)), has_value_(true) {}
Expected(E error) : data_(std::move(error)), has_value_(false) {}
bool has_value() const { return has_value_; }
T value() && { return std::get<T>(data_); }
E error() && { return std::get<E>(data_); }
private:
std::variant<T, E> data_;
bool has_value_;
};
// Example usage of Expected
Expected<int, std::string> divide(int a, int b) {
if (b == 0) {
return Expected<int, std::string>("Division by zero");
}
return Expected<int, std::string>(a / b);
}
int main() {
auto result = divide(10, 2);
if (result.has_value()) {
std::cout << "Result: " << result.value() << std::endl;
} else {
std::cout << "Error: " << result.error() << std::endl;
}
auto errorResult = divide(10, 0);
if (errorResult.has_value()) {
std::cout << "Result: " << errorResult.value() << std::endl;
} else {
std::cout << "Error: " << errorResult.error() << std::endl;
}
return 0;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?