<std::size_t I, typename ...Ts>
using type_pack_element = ...
| From: Andrey Davydov Sent: Thursday, October 11, 2018 3:05 PM To: ISO C++ Standard - Future Proposals Reply To: std-pr...@isocpp.org Subject: [std-proposals] std::type_pack_element |
template <size_t I, typename... Ts>
using type_pack_element = mp_at_c<mp_list<Ts...>, I>;Can you show an example of use?Sent from my BlackBerry portable Babbage Device
From: Andrey DavydovSent: Thursday, October 11, 2018 3:05 PMTo: ISO C++ Standard - Future ProposalsReply To: std-pr...@isocpp.orgSubject: [std-proposals] std::type_pack_elementIt's often needed to index into template parameter pack, when working with variadic templates, for instance in implementation of std::get<size_t> of tuple and variant. I wonder if there is any proposal to add metafunction<std::size_t I, typename ...Ts>
using type_pack_element = ...It can be implemented using existing language facilities or, similarly to std::make_integer_sequence, using compiler intrinsic. It's already implemented in Clang (__type_pack_element), and IMO it should not be hard to support in other compilers. Moreover it should be much more efficient from compile-time perspective because variadic pack is stored in some vector-like data structure, and consequently to get element by index from it is much cheaper than class template instantiation, overload resolution, etc...
template<typename... Ts, size_t... J>
class tuple_impl<index_sequence<J...>, Ts...> : element_storage<Ts, J>...
{
template<size_t I> friend decltype(auto) get(tuple & t)
{
using element_type = type_pack_element<I, Ts...>;
return static_cast<element_storage<element_type, I> &>(t).get();
}
}; On Thursday, October 11, 2018 at 2:05:37 PM UTC-5, Andrey Davydov wrote:It's often needed to index into template parameter pack, when working with variadic templates, for instance in implementation of std::get<size_t> of tuple and variant. I wonder if there is any proposal to add metafunction<std::size_t I, typename ...Ts>
using type_pack_element = ...It can be implemented using existing language facilities or, similarly to std::make_integer_sequence, using compiler intrinsic. It's already implemented in Clang (__type_pack_element), and IMO it should not be hard to support in other compilers. Moreover it should be much more efficient from compile-time perspective because variadic pack is stored in some vector-like data structure, and consequently to get element by index from it is much cheaper than class template instantiation, overload resolution, etc...
What you're looking for is called mp_at_c in the proposal, except it takes a "list" as the first parameter instead of a trailing template parameter. So yours implemented in terms of that one would just be:template <size_t I, typename... Ts>
using type_pack_element = mp_at_c<mp_list<Ts...>, I>;
This is already in Boost as the Mp11 library, if you want to just use it.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/0004bbca-d6eb-4db2-8243-a5d31b80a1ba%40isocpp.org.
Implemented in Clang now __type_pack_element supports SFINAE (https://godbolt.org/z/g_pIpH), but std::tuple_element_t doesn't.Do you see some cases where support of SFINAE would be useful?
Sorry for being late in the game. I originally implemented __type_pack_element in Clang as a workaround for the lack of efficient ways to index parameter packs in the context of template based metaprogramming. At this moment, I do not think that standardizing this facility is a good idea.C++ wants to move away of template-based metaprogramming and into constexpr value metaprogramming. This is a rather long journey, but the idea is that std::vector should be your type list of the future. In a world like that, indexing a type list can be done with operator[] on the vector, and that should be O(1) compile-time.Just my .02,Louis
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/a9f1d81b-bc39-4659-ba14-42a2fa354c3d%40isocpp.org.
You received this message because you are subscribed to a topic in the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this topic, visit https://groups.google.com/a/isocpp.org/d/topic/std-proposals/HRx31Xhg7CU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAPuuy5eWjTo2yPyfC1NJiFVC8_ae%2B6uVk_eD_jnxsC221ZLhMg%40mail.gmail.com.
Sorry for being late in the game. I originally implemented __type_pack_element in Clang as a workaround for the lack of efficient ways to index parameter packs in the context of template based metaprogramming. At this moment, I do not think that standardizing this facility is a good idea.C++ wants to move away of template-based metaprogramming and into constexpr value metaprogramming. This is a rather long journey, but the idea is that std::vector should be your type list of the future. In a world like that, indexing a type list can be done with operator[] on the vector, and that should be O(1) compile-time.
template<size_t I, typename...Ts>
using type_pack_element = __type_pack_element<I, Ts...>;
template<typename...Ts>
constexpr metatype metatypes[] = { $reflect(Ts)... };
template<size_t I, typename...Ts>
using type_pack_element = $unreflect(metatypes<Ts...>[i]);