Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T, int N, typename... Ts>
- struct get_index_in_pack_impl;
- template<typename T, int N, typename T0, typename... Ts>
- struct get_index_in_pack_impl<T, N, T0, Ts...>
- {
- static constexpr int value = std::is_same_v<T, T0> ? N : get_index_in_pack_impl<T, (N+1), Ts...>::value;
- };
- template<typename T, int N, typename T0>
- struct get_index_in_pack_impl<T, N, T0>
- {
- static constexpr int value = std::is_same_v<T, T0> ? N : -1;
- };
- template<typename T, int N>
- struct get_index_in_pack_impl<T, N>
- {
- static constexpr int value = -1;
- };
- template<size_t I, typename... Pack>
- struct n_type_in_pack_impl;
- template<size_t I, typename T, typename... Pack>
- struct n_type_in_pack_impl<I, T, Pack...>
- {
- using type = typename n_type_in_pack_impl<I - 1, Pack...>::type;
- };
- template<typename T, typename... Pack>
- struct n_type_in_pack_impl<0, T, Pack...>
- {
- using type = T;
- };
- template<typename T>
- struct n_type_in_pack_impl<0, T>
- {
- using type = T;
- };
- template<size_t I, typename... Pack>
- using GetTypeInPack = typename n_type_in_pack_impl<I, Pack...>;
- template<typename T, typename... Ts>
- using GetIndexInPack = get_index_in_pack_impl < T, 0, Ts... >;
- template<typename... Cmps>
- struct CmpGroup
- {
- std::array<void*, sizeof...(Cmps)> cmps;
- template<typename C>
- C& get()
- {
- constexpr int index = GetIndexInPack<C, Cmps...>::value;
- static_assert(index != -1, "");
- return *static_cast<C*>(index);
- }
- template<size_t I>
- auto& get()
- {
- static_assert(I < sizeof...(Cmps), "");
- using CmpType = typename GetTypeInPack<I, Cmps...>::type;
- return *static_cast<CmpType*>(cmps[I]);
- }
- };
- namespace std
- {
- template<typename... Types>
- struct tuple_size<CmpGroup<Types...>> : public integral_constant<size_t, sizeof...(Types)>;
- template<std::size_t N, typename... Types>
- struct tuple_element<N, CmpGroup<Types...>> {
- //got this from: https://blog.tartanllama.xyz/structured-bindings/
- using type = decltype(std::declval<CmpGroup<Types...>>().template get<N>());
- };
- }
- int main()
- {
- using namespace std;
- CmpGroup<int, double, float> cmp_group;
- auto[i, d, f] = cmp_group;
- //should not print anything but should compile
- cout << i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement