Skip to content

Commit e5dfb0d

Browse files
authored
Create meta_index_of.hpp
1 parent f0658f0 commit e5dfb0d

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

meta_index_of.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
/*
4+
synopsis
5+
6+
template<class>
7+
struct tag; // not defined
8+
9+
template<std::size_t I, class T>
10+
struct type_to_index;
11+
12+
template<class T, class, class...>
13+
struct meta_index_of_impl; // for internal use
14+
15+
template<class T, class... Args>
16+
struct meta_index_of;
17+
*/
18+
#include <utility>
19+
20+
template<class> struct tag;
21+
22+
template<std::size_t I, class T>
23+
struct type_to_index {
24+
std::integral_constant<std::size_t, I> operator()(T);
25+
};
26+
27+
template<class, class, class...> struct meta_index_of_impl;
28+
29+
template<class T, std::size_t... Idx, class... Args>
30+
struct meta_index_of_impl<T, std::index_sequence<Idx...>, Args...>
31+
: type_to_index<Idx, tag<Args>**>...
32+
{
33+
using type_to_index<Idx, tag<Args>**>::operator()...;
34+
static const std::size_t value;
35+
};
36+
template<class T, std::size_t... Idx, class... Args>
37+
inline constexpr std::size_t
38+
meta_index_of_impl<T, std::index_sequence<Idx...>, Args...>::value =
39+
decltype(meta_index_of_impl{}(static_cast<tag<T>**>(nullptr)))::value;
40+
41+
// Given a type T followed by a sequence of types, the trait meta_index_of
42+
// computes the 0-based index of T in the sequence. It is required that
43+
// T is in the sequence and the sequence does not contain duplicated types.
44+
template<class T, class... Args>
45+
struct meta_index_of
46+
: std::integral_constant<std::size_t,
47+
meta_index_of_impl<T, std::index_sequence_for<Args...>, Args...>::value
48+
> {};

0 commit comments

Comments
 (0)