https://github.com/9inefold/variant
Custom variant implementation for c++20
https://github.com/9inefold/variant
cpp cpp20 template-metaprogramming template-project
Last synced: 7 months ago
JSON representation
Custom variant implementation for c++20
- Host: GitHub
- URL: https://github.com/9inefold/variant
- Owner: 9inefold
- License: mit
- Created: 2023-02-04T06:41:23.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-04T06:42:54.000Z (about 3 years ago)
- Last Synced: 2025-06-12T17:45:36.354Z (9 months ago)
- Topics: cpp, cpp20, template-metaprogramming, template-project
- Language: C++
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# variant
This is my own implementation of ``std::variant`` for c++20.
Instead of using a recursive union, it uses pointers to represent the various possible states.
It uses multiple inheritance of a "node" structure to call the active overload.
## Usage
``variant`` can be used very similarly to ``std::variant``.
Here is an example of how it can be used:
```cpp
variant v1 { "Hello world!" };
v1.visit([](auto&& a) { std::cout << a << std::endl; });
v1.emplace(1);
std::cout << "at int: " << v1[Tp] << std::endl;
variant v2 { std::move(v1) };
v2.visit([](auto&& a) { std::cout << a << std::endl; });
v2.emplace("std::string is active!");
if(v2.contains())
std::cout << v2[Tp] << std::endl;
```
## Notes
You cannot use multiple of the same type in the variant,
as there are several overloads that would be ambiguous.
As always, if you have any issues, feel free to let me know!