A cleaner and more intuitive std::variant alternative

[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

✔️ Single header file, C++ 17
✔️ STL interface support (std::get, std::get_if, std::visit etc..)
✔️ Cleaner and less verbose interface (see examples below)
✔️ Lower memory footprint (uses smart index type based on type count)
✔️ Allows custom data alignment
✔️ Full constexpr support
✔️ Small template code generation
✔️ Fast compile times
✔️ 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:

if(std::holds_alternative<int>(variant))
 ...

With stdex::variant:

if(variant.holds_alternative<int>)
 ...

Checking element type and value without exceptions

With std::variant:

if(std::holds_alternative<int>(variant) && std::get<int>(variant) == 3)
 ...

With stdex::variant:

if(variant.holds_value<int>(3))
 ...

Since the type can be elided from the literal, we can even write:

if(variant.holds_value(3))
 ...

Getting the value directly

With std::variant:

int value = std::get<int>(variant);

With stdex::variant using std::optional:

std::optional<int> value = variant.get<int>();

With stdex::variant using a default value on type mismatch:

// Returns the default value of int (0) when the types do not match:
int value = variant.get_or_default<int>();

With stdex::variant using a custom value on type mismatch:

 // Returns 10 when the types do not match:
int value = variant.get_or_custom_value<int>(10);

With stdex::variant using a lambda:

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

Converting to std::tuple

With stdex::variant:

stdex::variant<int, float> variant{};
std::tuple<int, float> tuple = variant.as_tuple();

Converting to std::variant

With stdex::variant:

stdex::variant<int, float> variant{};
std::variant<int, float> tuple = variant.as_std();

Visiting types

With std::variant using the overload pattern:

template <typename... Ts> struct overload : Ts... { using Ts::operator()...; };
template <typename... Ts> overload(Ts...) -> overload<Ts...>;

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

With stdex::variant using visit:

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

Getting the index at compile time

With stdex::variant:

auto indexOfInt = stdex::variant<int, float>::index_of<int>();

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!

Owner
pinsrq
software engineer, hacker, game developer, shader programmer, youtuber, C, C++, C#, Rust, X86 Assembly, GLSL, Java, Python
pinsrq
Similar Resources

Upgraded from Pixar postcard path tracing, instead of printing Pixar, this program print my name, which pose to be more challenging than the original code. The upgraded is also more readable and run 9x faster than the original source code.

Upgraded from Pixar postcard path tracing, instead of printing Pixar, this program print my name, which pose to be more challenging than the original code. The upgraded is also more readable and run 9x faster than the original source code.

SDF-Sphere-Tracing Upgraded from Pixar postcard path tracing, instead of printing Pixar, this program print my name, which pose to be more challenging

Aug 29, 2022

3D scanning is becoming more and more ubiquitous.

Welcome to the MeshLib! 3D scanning is becoming more and more ubiquitous. Robotic automation, self-driving cars and multitude of other industrial, med

Sep 14, 2022

InlineExecute-Assembly is a proof of concept Beacon Object File (BOF) that allows security professionals to perform in process .NET assembly execution as an alternative to Cobalt Strikes traditional fork and run execute-assembly module

InlineExecute-Assembly InlineExecute-Assembly is a proof of concept Beacon Object File (BOF) that allows security professionals to perform in process

Sep 12, 2022

An open source alternative to and inspired by IOBit Unlocker

An open source alternative to and inspired by IOBit Unlocker

EzUnlock An open source alternative to and inspired by IOBit Unlocker EzUnlock is a simple GUI application for deleting stuck / stale / undeletable fi

Aug 9, 2022

Minipops alternative firmware for Music Thing Modular Radio Music

RMoxy Minipops drummer alternative firmware for Music Thing Modular Radio Music module The repository for the Radio Music module by Tom Whitwell Kits

Apr 6, 2022

Alternative openhaystack firmware

OpenHaystack - (ultra low power) alternative firmware This is an alternative OpenHaystack firmware based on https://github.com/seemoo-lab/openhaystack

Sep 3, 2022

BKM-10Rduino is an Arduino based alternative for the Sony BKM-10R control unit typically used with Sony broadcast monitors like the BVM-D20F1a/e/u

BKM-10Rduino is an Arduino based alternative for the Sony BKM-10R control unit typically used with Sony broadcast monitors like the BVM-D20F1a/e/u

BKM-10Rduino BKM-10Rduino is an Arduino (natch) based alternative for the Sony BKM-10R control unit typically used with Sony broadcast monitors like t

Apr 11, 2022

An alternative mlatu implementation, written in C

Readable Mlatu An implementation of Mlatu written in C Some Quiche Eaters have been complaining that code here is illegible, so I have named it "Reada

May 5, 2022

Example code for the research paper "Masked Software Occlusion Culling"; implements an efficient alternative to the hierarchical depth buffer algorithm.

MaskedOcclusionCulling This code accompanies the research paper "Masked Software Occlusion Culling", and implements an efficient alternative to the hi

Sep 24, 2022
high performance C++20 implementation of std::variant

A minimal compile-time overhead, C++20 implementation of std::variant. Fully standard conforming with a couple of documented differences.

Sep 16, 2022
Invoke.hpp - std::invoke/std::apply analogs for C++11/14

invoke.hpp std::invoke/std::apply analogs for C++11/14 Requirements gcc >= 4.9 clang >= 3.8 msvc >= 2015 Installation invoke.hpp is a header-only libr

Jun 30, 2022
Full Apex/EAC/Origin Trace Files Cleaner

Apex Cleaner Full Apex/EAC/Origin Trace Files Cleaner This is the best cleaner I've ever made. So this is a Full Apex Legends trace cleaner. Mostly my

Sep 17, 2022
Linux SOCD cleaner

Linux SOCD cleaner A (less feature rich) Linux version of SOCD cleaner. Currently this only implements the basic functionality without things such as

Mar 25, 2022
Arbitrary Precision provides C++ long integer types that behave as basic integer types. This library aims to be intuitive and versatile in usage, rather than fast.

Arbitrary Precision (AP) Cross-platform and cross-standard header-only arbitrary precision arithmetic library. Currently it offers integer types that

Aug 3, 2022
An IoT based project using Wi-Fi Module and MIT App Inventor with intuitive control
An IoT based project using Wi-Fi Module and MIT App Inventor with intuitive control

Phone Controlled (joystick) Robot Car Project An IoT based project using Wi-Fi Module and MIT App Inventor with intuitive control. A 4 wheeler robot c

Jan 20, 2022
Intuitive & Powerful C++20 consteval metaprogramming library(via value).

meta-value-list This library provides a bunch of consteval toolsets to do metaprogramming, and provides the pipeline syntactic sugar for function comb

Sep 15, 2022
Intuitive & Powerful C++20 consteval metaprogramming library(via value).

meta-list This library provides a bunch of consteval toolsets to do metaprogramming, and provides the pipeline syntactic sugar for function combinatio

Sep 15, 2022
EDK2 port for Lumia 630 & it's 512mb variants. 1gb variant coming soon.

Lumia930Pkg WIP Custom ARM UEFI firmware for Lumia930 Current Status EMMC MMU PMIC GPIO Working Linux Notes Linux kernel do boots but crashes pretty s

Jan 2, 2022
Any - A simple variant type for C++

ANY Variant type for C++ This is similar to Boost.Any in that any type can be encapsulated and retrieved, but this implementation does not have any de

Oct 24, 2020