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

https://github.com/mariosieg/extendedvariant

A cleaner and more intuitive std::variant alternative
https://github.com/mariosieg/extendedvariant

Last synced: about 1 year ago
JSON representation

A cleaner and more intuitive std::variant alternative

Awesome Lists containing this project

README

          

# [WIP] ExtendedVariant
This single header library is part of my C++ extended standard ```stdex``` libraries. Check our my profile for more.

Working with C++ 17's ```std::variant``` can be cumbersome and verbose.

This single header library contains the alternative ```stdex::variant```,

which has the same interface as ```std::variant``` and usually works as a drop-in replacement,

but has a cleaner interface and more goodies.

Features

:heavy_check_mark: Single header file, C++ 17

:heavy_check_mark: STL interface support (```std::get```, ```std::get_if```, ```std::visit``` etc..)

:heavy_check_mark: Cleaner and less verbose interface (see examples below)

:heavy_check_mark: Lower memory footprint (uses smart index type based on type count)

:heavy_check_mark: Allows custom data alignment

:heavy_check_mark: Full ```constexpr``` support

:heavy_check_mark: Small template code generation

:heavy_check_mark: Fast compile times

:heavy_check_mark: Bonus functions and methods (see examples below)

Usage

Just copy the ```extended_variant.hpp``` file into your source code, that's it.

Please remember to include the ```LICENSE``` file according to the license agreement.

Examples

Checking element type

With ```std::variant```:

```cpp
if(std::holds_alternative(variant))
...
```

With ```stdex::variant```:
```cpp
if(variant.holds_alternative)
...
```

Checking element type and value without exceptions

With ```std::variant```:

```cpp
if(std::holds_alternative(variant) && std::get(variant) == 3)
...
```

With ```stdex::variant```:
```cpp
if(variant.holds_value(3))
...
```
Since the type can be elided from the literal, we can even write:
```cpp
if(variant.holds_value(3))
...
```

Getting the value directly

With ```std::variant```:

```cpp
int value = std::get(variant);
```

With ```stdex::variant``` using ```std::optional```:
```cpp
std::optional value = variant.get();
```

With ```stdex::variant``` using a default value on type mismatch:
```cpp
// Returns the default value of int (0) when the types do not match:
int value = variant.get_or_default();
```

With ```stdex::variant``` using a custom value on type mismatch:
```cpp
// Returns 10 when the types do not match:
int value = variant.get_or_custom_value(10);
```

With ```stdex::variant``` using a lambda:
```cpp
// Invokes the lambda and returns 20 when the types do not match:
int value = variant.get_or_invoke([]() -> int { return 10 + 10; });
```

Converting to std::tuple

With ```stdex::variant```:

```cpp
stdex::variant variant{};
std::tuple tuple = variant.as_tuple();
```

Converting to std::variant

With ```stdex::variant```:

```cpp
stdex::variant variant{};
std::variant tuple = variant.as_std();
```

Visiting types

With ```std::variant``` using the overload pattern:

```cpp
template struct overload : Ts... { using Ts::operator()...; };
template overload(Ts...) -> overload;

std::variant variant{};
std::visit
(
overload
{
[](int) { std::cout << "integer"; },
[](float) { std::cout << "floating point"; },
},
variant
);
```

With ```stdex::variant``` using ```visit```:

```cpp
stdex::variant variant{};
variant.visit
(
[](int) { std::cout << "integer"; },
[](float) { std::cout << "floating point"; }
);
```

Getting the index at compile time

With ```stdex::variant```:

```cpp
auto indexOfInt = stdex::variant::index_of();
```

Contributing

This library is not finished yet,
so it's **very** open to contributions!

Everybody is welcome, just create an issue or submit your PR!