Skip to content

Instantly share code, notes, and snippets.

@obidavis
obidavis / variant_magic.hpp
Last active May 29, 2023 15:24
Construct a variant alternative from a runtime index. Uses a static constexpr table to promote runtime integers to std::integral_constants, and forwards these along with args to the variant alternative's constructor. Useful for deserialisation? TODO: bounds checks, how to handle invalid indices...
#pragma once
#include <utility>
#include <variant>
/**
* @brief Gives the template `B` specialised with
* the parameter pack of the specialised template `A`.
* e.g. if `A` is a `std::tuple<int, float>` and `B` is
* a `std::variant`, `rebind_t<A, B>` will be a