An open API service indexing awesome lists of open source software.

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

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!