[P0323R12] introduced class template std::expected<T, E>,| www.open-std.org
In one of the most respected stackoverflow answer I found an example of std::expected template class usages: What are coroutines in C++20? At the same time I cannot find any mentioning of this clas...| Stack Overflow
int main() { std::expected<int, std::string> def; std::cout << def.has_value() << ", " << def.value() << '\n'; }| godbolt.org
struct DatabaseRecord { bool valid; }; struct Database { DatabaseRecord query(int id) { return DatabaseRecord{.valid = id == 10}; } bool connected() { return true; } }; std::expected<DatabaseRecord, std::string> findRecord(Database& db, int recordId) { if (!db.connected()) return std::unexpected("DB not connected"); auto record = db.query(recordId); if (record.valid) return record; return std::unexpected("Record not found"); } int main() { Database myDatabase; auto result = findRecord(myDatab...| godbolt.org
int main() { std::expected<std::vector<int>, std::string> success(std::in_place, {1, 2, 3}); std::expected<std::pair<bool, bool>, int> pack(std::in_place, true, false); }| godbolt.org
int main() { std::expected<int, std::string> error{std::unexpect, "error"}; }| godbolt.org
int main() { std::expected<int, std::string> success{42}; std::cout << success.has_value() << ", " << success.value() << '\n'; }| godbolt.org
int main() { std::expected<int, std::string> result{std::unexpected("")}; int value = result.value_or(0); // Returns 0 if result holds an error std::cout << "Value: " << value << '\n'; }| godbolt.org
std::expected<int, std::string> convertToInt(const std::string& input) { int value{}; auto [ptr, ec] = std::from_chars(input.data(), input.data() + input.size(), value); if (ec == std::errc()) return value; if (ec == std::errc::invalid_argument) return std::unexpected("Invalid number format"); else if (ec == std::errc::result_out_of_range) return std::unexpected("Number out of range"); return std::unexpected("Unknown conversion error"); } int main() { std::string userInput = "111111111111111"...| godbolt.org
std::expected<void, std::string> performAction(bool condition) { if (condition) return {}; else return std::unexpected("Action failed"); } int main() { if (auto result = performAction(false); result) std::cout << "Action performed successfully\n"; else std::cout << "Error: " << result.error() << '\n'; }| godbolt.org
int main() { std::expected<int, std::string> result{std::unexpected{"err"}}; if (!result) { result.error() += std::string{" in the filesystem"}; std::cout << result.error(); } }| godbolt.org
int main() { std::expected<int, std::string> result{std::unexpected("")}; try { std::cout << "Value: " << result.value() << '\n'; } catch (const std::bad_expected_access<std::string>& e) { std::cout << "Caught error: " << e.what() << '\n'; } }| godbolt.org
In the article about std::expected, I introduced the type and showed some basic examples, and in this text, you’ll learn how it is implemented. A simple idea with struct In short, std::expected should contain two data members: the actual expected value and the unexpected error object. So, in theory, we could use a simple structure:| C++ Stories
In this post we’ll have a look at new operations added to std::optional in C++23. These operations, inspired by functional programming concepts, offer a more concise and expressive way to work with optional values, reducing boilerplate and improving code readability. Let’s meet and_then(), transform() and or_else(), new member functions. Traditional Approach with if/else and optional C++20 In C++20 when you work with std::optional you have to rely heavily on conditional checks to ensur...| C++ Stories