From c6912618a42e1d8bced47958b3ad598e4f8a24fc Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 12 Aug 2020 10:18:23 +1000 Subject: [PATCH] typeidx: add variant query --- typeidx.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/typeidx.hpp b/typeidx.hpp index d2589062..5855173b 100644 --- a/typeidx.hpp +++ b/typeidx.hpp @@ -9,6 +9,7 @@ #pragma once #include +#include namespace cruft { @@ -49,4 +50,21 @@ namespace cruft { static auto id = detail::typeidx_next (); return id; } + + + /// Returns the typeidx of the contained value within a variant. + /// + /// May throw std::bad_variant_access if the variant is + /// valueless_by_exception. + template + int + value_typeidx (std::variant const &val) + { + return std::visit ( + [] (ValueT const &) { + return typeidx (); + }, + val + ); + } }