{"id":13494862,"url":"https://github.com/TartanLlama/optional","last_synced_at":"2025-03-28T15:32:01.929Z","repository":{"id":37677753,"uuid":"105306008","full_name":"TartanLlama/optional","owner":"TartanLlama","description":"C++11/14/17 std::optional with functional-style extensions and reference support","archived":false,"fork":false,"pushed_at":"2024-06-10T20:36:47.000Z","size":468,"stargazers_count":880,"open_issues_count":28,"forks_count":71,"subscribers_count":27,"default_branch":"master","last_synced_at":"2025-03-24T04:36:52.332Z","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,"publiccode":null,"codemeta":null}},"created_at":"2017-09-29T18:44:22.000Z","updated_at":"2025-03-13T14:37:42.000Z","dependencies_parsed_at":"2022-09-15T08:50:22.689Z","dependency_job_id":"52306879-b10d-4740-9480-340bbb176016","html_url":"https://github.com/TartanLlama/optional","commit_stats":{"total_commits":246,"total_committers":16,"mean_commits":15.375,"dds":0.4512195121951219,"last_synced_commit":"3a1209de8370bf5fe16362934956144b49591565"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TartanLlama%2Foptional","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TartanLlama%2Foptional/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TartanLlama%2Foptional/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TartanLlama%2Foptional/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TartanLlama","download_url":"https://codeload.github.com/TartanLlama/optional/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246053811,"owners_count":20716260,"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-07-31T19:01:28.909Z","updated_at":"2025-03-28T15:32:01.888Z","avatar_url":"https://github.com/TartanLlama.png","language":"C++","readme":"# optional\n\nSingle header implementation of `std::optional` with functional-style extensions and support for references.\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://travis-ci.org/TartanLlama/optional.png?branch=master)](https://travis-ci.org/TartanLlama/optional)\nMSVC: [![Windows Build Status](https://ci.appveyor.com/api/projects/status/k5x00xa11y3s5wsg?svg=true)](https://ci.appveyor.com/project/TartanLlama/optional)\n\n`std::optional` is the preferred way to represent an object which may or may not have a value. Unfortunately, chaining together many computations which may or may not produce a value can be verbose, as empty-checking code will be mixed in with the actual programming logic. This implementation provides a number of utilities to make coding with `optional` cleaner.\n\nFor example, instead of writing this code:\n\n```c++\nstd::optional\u003cimage\u003e get_cute_cat (const image\u0026 img) {\n    auto cropped = crop_to_cat(img);\n    if (!cropped) {\n      return std::nullopt;\n    }\n\n    auto with_tie = add_bow_tie(*cropped);\n    if (!with_tie) {\n      return std::nullopt;\n    }\n\n    auto with_sparkles = make_eyes_sparkle(*with_tie);\n    if (!with_sparkles) {\n      return std::nullopt;\n    }\n\n    return add_rainbow(make_smaller(*with_sparkles));\n}\n```\n\nYou can do this:\n\n```c++\ntl::optional\u003cimage\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::optional`, 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::optional\u003cstd::size_t\u003e s = opt_string.map(\u0026std::string::size);`\n- `and_then`: like `map`, but for operations which return a `tl::optional`.\n  * `tl::optional\u003cint\u003e stoi (const std::string\u0026 s);`\n  * `tl::optional\u003cint\u003e i = opt_string.and_then(stoi);`\n- `or_else`: calls some function if there is no value stored.\n  * `opt.or_else([] { throw std::runtime_error{\"oh no\"}; });`\n- `map_or`: carries out a `map` if there is a value, otherwise returns a default value.\n  * `tl::optional\u003cstd::size_t\u003e s = opt_string.map_or(\u0026std::string::size, 0);`\n- `map_or_else`: carries out a `map` if there is a value, otherwise returns the result of a given default function.\n  * `std::size_t get_default();`\n  * `tl::optional\u003cstd::size_t\u003e s = opt_string.map_or_else(\u0026std::string::size, get_default);`\n- `conjunction`: returns the argument if a value is stored in the optional, otherwise an empty optional.\n  * `tl::make_optional(42).conjunction(13); //13`\n  * `tl::optional\u003cint\u003e{}.conjunction(13); //empty`\n- `disjunction`: returns the argument if the optional is empty, otherwise the current value.\n  * `tl::make_optional(42).disjunction(13); //42`\n  * `tl::optional\u003cint\u003e{}.disjunction(13); //13`\n- `take`: returns the current value, leaving the optional empty.\n  * `opt_string.take().map(\u0026std::string::size); //opt_string now empty;`\n\nIn addition to those member functions, optional references are also supported:\n\n```c++\nint i = 42;\ntl::optional\u003cint\u0026\u003e o = i;\n*o == 42; //true\ni = 12;\n*o == 12; //true\n\u0026*o == \u0026i; //true\n```\n\nAssignment has rebind semantics rather than assign-through semantics:\n\n```c++\nint j = 8;\no = j;\n\n\u0026*o == \u0026j; //true\n```\n\n### Compiler support\n\nTested on:\n\n\n- Linux\n  * clang 6.0.1\n  * clang 5.0.2\n  * clang 4.0.1\n  * clang 3.9\n  * clang 3.8\n  * clang 3.7\n  * clang 3.6\n  * clang 3.5\n  * g++ 8.0.1\n  * g++ 7.3\n  * g++ 6.4\n  * g++ 5.5\n  * g++ 4.9\n  * g++ 4.8\n- Windows\n  * MSVC 2015\n  * MSVC 2017\n\n### Standards Proposal\n\nThis library also serves as an implementation of WG21 standards paper [P0798R0: Monadic operations for std::optional](https://wg21.tartanllama.xyz/monadic-optional). This paper proposes adding `map`, `and_then`, and `or_else` to `std::optional`.\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 `optional` library. This work is published from: United Kingdom.\n","funding_links":[],"categories":["C++","Standard/Support Libraries"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTartanLlama%2Foptional","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTartanLlama%2Foptional","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTartanLlama%2Foptional/lists"}