typeidx: add variant query

This commit is contained in:
Danny Robson 2020-08-12 10:18:23 +10:00
parent 8bfb1e3d92
commit c6912618a4

View File

@ -9,6 +9,7 @@
#pragma once
#include <atomic>
#include <variant>
namespace cruft {
@ -49,4 +50,21 @@ namespace cruft {
static auto id = detail::typeidx_next<TagT> ();
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 <typename TagT=void, typename ...ComponentT>
int
value_typeidx (std::variant<ComponentT...> const &val)
{
return std::visit (
[] <typename ValueT> (ValueT const &) {
return typeidx<ValueT> ();
},
val
);
}
}