{"id":13730571,"url":"https://github.com/TartanLlama/expected","last_synced_at":"2025-05-08T03:30:56.477Z","repository":{"id":27035422,"uuid":"108308364","full_name":"TartanLlama/expected","owner":"TartanLlama","description":"C++11/14/17 std::expected with functional-style extensions ","archived":false,"fork":false,"pushed_at":"2024-01-09T21:53:48.000Z","size":1920,"stargazers_count":1537,"open_issues_count":63,"forks_count":133,"subscribers_count":35,"default_branch":"master","last_synced_at":"2024-11-05T03:36:43.277Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://tl.tartanllama.xyz","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TartanLlama.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2017-10-25T18:19:53.000Z","updated_at":"2024-11-01T23:34:21.000Z","dependencies_parsed_at":"2023-01-16T21:45:18.162Z","dependency_job_id":"cebb8266-6af7-4367-bbbe-70030a622cee","html_url":"https://github.com/TartanLlama/expected","commit_stats":{"total_commits":200,"total_committers":22,"mean_commits":9.090909090909092,"dds":"0.44999999999999996","last_synced_commit":"aa4f7a5e2422169d48342e369597f463ab666558"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TartanLlama%2Fexpected","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TartanLlama%2Fexpected/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TartanLlama%2Fexpected/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TartanLlama%2Fexpected/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TartanLlama","download_url":"https://codeload.github.com/TartanLlama/expected/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224196544,"owners_count":17271855,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T02:01:16.705Z","updated_at":"2025-05-08T03:30:56.467Z","avatar_url":"https://github.com/TartanLlama.png","language":"C++","funding_links":[],"categories":["Standard/Support Libraries","C++"],"sub_categories":[],"readme":"# expected\nSingle header implementation of `std::expected` with functional-style extensions.\n\n[![Documentation Status](https://readthedocs.org/projects/tl-docs/badge/?version=latest)](https://tl.tartanllama.xyz/en/latest/?badge=latest)\nClang + GCC: [![Linux Build Status](https://github.com/TartanLlama/expected/actions/workflows/cmake.yml/badge.svg)](https://github.com/TartanLlama/expected/actions/workflows/cmake.yml)\nMSVC: [![Windows Build Status](https://ci.appveyor.com/api/projects/status/k5x00xa11y3s5wsg?svg=true)](https://ci.appveyor.com/project/TartanLlama/expected)\n\nAvailable on [Vcpkg](https://github.com/microsoft/vcpkg/tree/master/ports/tl-expected), [Conan](https://github.com/yipdw/conan-tl-expected) and [`build2`](https://cppget.org/tl-expected).\n\n[`std::expected`](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0323r3.pdf) is proposed as the preferred way to represent object which will either have an expected value, or an unexpected value giving information about why something failed. Unfortunately, chaining together many computations which may fail can be verbose, as error-checking code will be mixed in with the actual programming logic. This implementation provides a number of utilities to make coding with `expected` cleaner.\n\nFor example, instead of writing this code:\n\n```cpp\nstd::expected\u003cimage,fail_reason\u003e get_cute_cat (const image\u0026 img) {\n    auto cropped = crop_to_cat(img);\n    if (!cropped) {\n      return cropped;\n    }\n\n    auto with_tie = add_bow_tie(*cropped);\n    if (!with_tie) {\n      return with_tie;\n    }\n\n    auto with_sparkles = make_eyes_sparkle(*with_tie);\n    if (!with_sparkles) {\n       return with_sparkles;\n    }\n\n    return add_rainbow(make_smaller(*with_sparkles));\n}\n```\n\nYou can do this:\n\n```cpp\ntl::expected\u003cimage,fail_reason\u003e get_cute_cat (const image\u0026 img) {\n    return crop_to_cat(img)\n           .and_then(add_bow_tie)\n           .and_then(make_eyes_sparkle)\n           .map(make_smaller)\n           .map(add_rainbow);\n}\n```\n\nThe interface is the same as `std::expected` as proposed in [p0323r3](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0323r3.pdf), but the following member functions are also defined. Explicit types are for clarity.\n\n- `map`: carries out some operation on the stored object if there is one.\n  * `tl::expected\u003cstd::size_t,std::error_code\u003e s = exp_string.map(\u0026std::string::size);`\n- `map_error`: carries out some operation on the unexpected object if there is one.\n  * `my_error_code translate_error (std::error_code);`\n  * `tl::expected\u003cint,my_error_code\u003e s = exp_int.map_error(translate_error);`\n- `and_then`: like `map`, but for operations which return a `tl::expected`.\n  * `tl::expected\u003cast, fail_reason\u003e parse (const std::string\u0026 s);`\n  * `tl::expected\u003cast, fail_reason\u003e exp_ast = exp_string.and_then(parse);`\n- `or_else`: calls some function if there is no value stored.\n  * `exp.or_else([] { throw std::runtime_error{\"oh no\"}; });`\n\np0323r3 specifies calling `.error()` on an expected value, or using the `*` or `-\u003e` operators on an unexpected value, to be undefined behaviour. In this implementation it causes an assertion failure. The implementation of assertions can be overridden by defining the macro `TL_ASSERT(boolean_condition)` before #including \u003ctl/expected.hpp\u003e; by default, `assert(boolean_condition)` from the `\u003ccassert\u003e` header is used. Note that correct code would not rely on these assertions.\n\n### Compiler support\n\nTested on:\n\n- Linux\n  * clang++ 3.5, 3.6, 3.7, 3.8, 3.9, 4, 5, 6, 7, 8, 9, 10, 11\n  * g++ 4.8, 4.9, 5.5, 6.4, 7.5, 8, 9, 10 \n- Windows\n  * MSVC 2015, 2017, 2019, 2022\n\n----------\n\n[![CC0](http://i.creativecommons.org/p/zero/1.0/88x31.png)](\"http://creativecommons.org/publicdomain/zero/1.0/\")\n\nTo the extent possible under law, [Sy Brand](https://twitter.com/TartanLlama) has waived all copyright and related or neighboring rights to the `expected` library. This work is published from: United Kingdom.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTartanLlama%2Fexpected","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTartanLlama%2Fexpected","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTartanLlama%2Fexpected/lists"}