{"id":24740594,"url":"https://github.com/mariosieg/extendedvariant","last_synced_at":"2025-03-22T19:21:12.795Z","repository":{"id":161602728,"uuid":"374231509","full_name":"MarioSieg/ExtendedVariant","owner":"MarioSieg","description":"A cleaner and more intuitive std::variant alternative","archived":false,"fork":false,"pushed_at":"2021-06-07T09:07:07.000Z","size":16,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-27T23:27:57.105Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MarioSieg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2021-06-05T23:37:26.000Z","updated_at":"2023-01-16T13:22:55.000Z","dependencies_parsed_at":"2023-05-22T06:30:37.871Z","dependency_job_id":null,"html_url":"https://github.com/MarioSieg/ExtendedVariant","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioSieg%2FExtendedVariant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioSieg%2FExtendedVariant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioSieg%2FExtendedVariant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarioSieg%2FExtendedVariant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MarioSieg","download_url":"https://codeload.github.com/MarioSieg/ExtendedVariant/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245008607,"owners_count":20546371,"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":"2025-01-27T23:24:44.092Z","updated_at":"2025-03-22T19:21:12.772Z","avatar_url":"https://github.com/MarioSieg.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [WIP] ExtendedVariant\nThis single header library is part of my C++ extended standard ```stdex``` libraries. Check our my profile for more.\u003cbr\u003e\nWorking with C++ 17's ```std::variant``` can be cumbersome and verbose.\u003cbr\u003e\nThis single header library contains the alternative ```stdex::variant```,\u003cbr\u003e\nwhich has the same interface as ```std::variant``` and usually works as a drop-in replacement,\u003cbr\u003e\nbut has a cleaner interface and more goodies.\u003cbr\u003e\n\n\u003ch2\u003e Features \u003c/h2\u003e\n\n:heavy_check_mark: Single header file, C++ 17\u003cbr\u003e\n:heavy_check_mark: STL interface support (```std::get```, ```std::get_if```, ```std::visit``` etc..)\u003cbr\u003e\n:heavy_check_mark: Cleaner and less verbose interface (see examples below)\u003cbr\u003e\n:heavy_check_mark: Lower memory footprint (uses smart index type based on type count)\u003cbr\u003e\n:heavy_check_mark: Allows custom data alignment\u003cbr\u003e\n:heavy_check_mark: Full ```constexpr``` support\u003cbr\u003e\n:heavy_check_mark: Small template code generation\u003cbr\u003e\n:heavy_check_mark: Fast compile times\u003cbr\u003e\n:heavy_check_mark: Bonus functions and methods (see examples below)\u003cbr\u003e\n\n\u003ch2\u003e Usage \u003c/h2\u003e\n\nJust copy the ```extended_variant.hpp``` file into your source code, that's it.\u003cbr\u003e\nPlease remember to include the ```LICENSE``` file according to the license agreement.\u003cbr\u003e\n\n\u003ch2\u003e Examples \u003c/h2\u003e\n\n\u003ch3\u003e Checking element type \u003c/h3\u003e\n\nWith ```std::variant```:\u003cbr\u003e\n```cpp\nif(std::holds_alternative\u003cint\u003e(variant))\n ...\n```\n \nWith ```stdex::variant```:\n```cpp\nif(variant.holds_alternative\u003cint\u003e)\n ...\n```\n\n\u003ch3\u003e Checking element type and value without exceptions \u003c/h3\u003e\n\nWith ```std::variant```:\u003cbr\u003e\n```cpp\nif(std::holds_alternative\u003cint\u003e(variant) \u0026\u0026 std::get\u003cint\u003e(variant) == 3)\n ...\n```\n \nWith ```stdex::variant```:\n```cpp\nif(variant.holds_value\u003cint\u003e(3))\n ...\n```\nSince the type can be elided from the literal, we can even write:\n```cpp\nif(variant.holds_value(3))\n ...\n```\n\n\u003ch3\u003e Getting the value directly \u003c/h3\u003e\n\nWith ```std::variant```:\u003cbr\u003e\n```cpp\nint value = std::get\u003cint\u003e(variant);\n```\n \nWith ```stdex::variant``` using ```std::optional```:\n```cpp\nstd::optional\u003cint\u003e value = variant.get\u003cint\u003e();\n```\n\nWith ```stdex::variant``` using a default value on type mismatch:\n```cpp\n// Returns the default value of int (0) when the types do not match:\nint value = variant.get_or_default\u003cint\u003e();\n```\n\nWith ```stdex::variant``` using a custom value on type mismatch:\n```cpp\n // Returns 10 when the types do not match:\nint value = variant.get_or_custom_value\u003cint\u003e(10);\n```\n\nWith ```stdex::variant``` using a lambda:\n```cpp\n// Invokes the lambda and returns 20 when the types do not match:\nint value = variant.get_or_invoke\u003cint\u003e([]() -\u003e int { return 10 + 10; });\n```\n\n\u003ch3\u003e Converting to std::tuple \u003c/h3\u003e\n\nWith ```stdex::variant```:\u003cbr\u003e\n```cpp\nstdex::variant\u003cint, float\u003e variant{};\nstd::tuple\u003cint, float\u003e tuple = variant.as_tuple();\n```\n\n\u003ch3\u003e Converting to std::variant \u003c/h3\u003e\n\nWith ```stdex::variant```:\u003cbr\u003e\n```cpp\nstdex::variant\u003cint, float\u003e variant{};\nstd::variant\u003cint, float\u003e tuple = variant.as_std();\n```\n\n\u003ch3\u003e Visiting types \u003c/h3\u003e\n\nWith ```std::variant``` using the overload pattern:\u003cbr\u003e\n```cpp\ntemplate \u003ctypename... Ts\u003e struct overload : Ts... { using Ts::operator()...; };\ntemplate \u003ctypename... Ts\u003e overload(Ts...) -\u003e overload\u003cTs...\u003e;\n\nstd::variant\u003cint, float\u003e variant{};\nstd::visit\n(\n\toverload\n\t{\n\t\t[](int) { std::cout \u003c\u003c \"integer\"; },\n\t\t[](float) { std::cout \u003c\u003c \"floating point\"; },\n\t}, \n\tvariant\n);\n```\n \nWith ```stdex::variant``` using ```visit```:\u003cbr\u003e\n```cpp\nstdex::variant\u003cint, float\u003e variant{};\nvariant.visit\n(\n\t[](int) { std::cout \u003c\u003c \"integer\"; },\n\t[](float) { std::cout \u003c\u003c \"floating point\"; }\n);\n```\n\n\u003ch3\u003e Getting the index at compile time \u003c/h3\u003e\n\nWith ```stdex::variant```:\u003cbr\u003e\n```cpp\nauto indexOfInt = stdex::variant\u003cint, float\u003e::index_of\u003cint\u003e();\n```\n\n\u003ch3\u003e Contributing \u003c/h3\u003e\n\nThis library is not finished yet,\nso it's **very** open to contributions!\u003cbr\u003e\nEverybody is welcome, just create an issue or submit your PR!\u003cbr\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmariosieg%2Fextendedvariant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmariosieg%2Fextendedvariant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmariosieg%2Fextendedvariant/lists"}