Discussion:
[std-proposals] std::type_pack_element
Andrey Davydov
2018-10-11 19:05:37 UTC
Permalink
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...
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/f2d44cc1-d626-4adb-9f4c-b4555ee4cb08%40isocpp.org.
Tony V E
2018-10-11 19:30:12 UTC
Permalink
<html><head></head><body lang="en-US" style="background-color: rgb(255, 255, 255); line-height: initial;"> <div style="width: 100%; font-size: initial; font-family: Calibri, 'Slate Pro', sans-serif, sans-serif; color: rgb(31, 73, 125); text-align: initial; background-color: rgb(255, 255, 255);">Can you show an example of use?</div> <div style="width: 100%; font-size: initial; font-family: Calibri, 'Slate Pro', sans-serif, sans-serif; color: rgb(31, 73, 125); text-align: initial; background-color: rgb(255, 255, 255);"><br style="display:initial"></div> <div style="font-size: initial; font-family: Calibri, 'Slate Pro', sans-serif, sans-serif; color: rgb(31, 73, 125); text-align: initial; background-color: rgb(255, 255, 255);">Sent&nbsp;from&nbsp;my&nbsp;BlackBerry&nbsp;portable&nbsp;Babbage&nbsp;Device</div> <table width="100%" style="background-color:white;border-spacing:0px;"> <tbody><tr><td colspan="2" style="font-size: initial; text-align: initial; background-color: rgb(255, 255, 255);"> <div style="border-style: solid none none; border-top-color: rgb(181, 196, 223); border-top-width: 1pt; padding: 3pt 0in 0in; font-family: Tahoma, 'BB Alpha Sans', 'Slate Pro'; font-size: 10pt;"> <div><b>From: </b>Andrey Davydov</div><div><b>Sent: </b>Thursday, October 11, 2018 3:05 PM</div><div><b>To: </b>ISO C++ Standard - Future Proposals</div><div><b>Reply To: </b>std-***@isocpp.org</div><div><b>Subject: </b>[std-proposals] std::type_pack_element</div></div></td></tr></tbody></table><div style="border-style: solid none none; border-top-color: rgb(186, 188, 209); border-top-width: 1pt; font-size: initial; text-align: initial; background-color: rgb(255, 255, 255);"></div><br><div id="_originalContent" style=""><div dir="ltr"><span style="font-size: small;">It's often needed to index into template parameter pack, when working with variadic templates, for instance in implementation of <font face="courier new, monospace">std::get&lt;size_t&gt;</font> of <font face="courier new, monospace">tuple</font> and <font face="courier new, monospace">variant</font>. I wonder if there is any proposal to add metafunction&nbsp;</span><div class="prettyprint" style="background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; overflow-wrap: break-word;"><code class="prettyprint"><div class="subprettyprint"><span style="color: #008;" class="styled-by-prettify">&lt;std::size_t</span><span style="color: #000;" class="styled-by-prettify"> </span><span style="color: #606;" class="styled-by-prettify">I</span><span style="color: #000;" class="styled-by-prettify">, </span><span style="color: #606;" class="styled-by-prettify">typename</span><span style="color: #000;" class="styled-by-prettify"> ...</span><span style="color: #606;" class="styled-by-prettify">Ts</span><span style="color: #008;" class="styled-by-prettify">&gt;</span><span style="color: #000;" class="styled-by-prettify"><br>using type_pack_element = ...<br></span></div></code></div><div><span style="font-size: small;">It</span><span style="font-size: small;">&nbsp;</span><span style="font-size: small;">can be implemented using existing language facilities or, similarly to <font face="courier new, monospace">std::make_integer_sequence</font>, using compiler intrinsic. It's already implemented in Clang (<font face="courier new, monospace">__type_pack_element</font>), 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...</span></div></div>

<p></p>

-- <br>
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-proposals+***@isocpp.org">std-proposals+***@isocpp.org</a>.<br>
To post to this group, send email to <a href="mailto:std-***@isocpp.org">std-***@isocpp.org</a>.<br>
To view this discussion on the web visit <a href="https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/f2d44cc1-d626-4adb-9f4c-b4555ee4cb08%40isocpp.org?utm_medium=email&amp;utm_source=footer">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/f2d44cc1-d626-4adb-9f4c-b4555ee4cb08%40isocpp.org</a>.<br>
<br><!--end of _originalContent --></div></body></html>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-proposals+***@isocpp.org">std-proposals+***@isocpp.org</a>.<br />
To post to this group, send email to <a href="mailto:std-***@isocpp.org">std-***@isocpp.org</a>.<br />
To view this discussion on the web visit <a href="https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/20181011193012.5218385.29683.63037%40gmail.com?utm_medium=email&utm_source=footer">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/20181011193012.5218385.29683.63037%40gmail.com</a>.<br />
Jonathan Müller
2018-10-11 19:36:09 UTC
Permalink
Post by Tony V E
Can you show an example of use?
Sent from my BlackBerry portable Babbage Device
*From: *Andrey Davydov
*Sent: *Thursday, October 11, 2018 3:05 PM
*To: *ISO C++ Standard - Future Proposals
*Subject: *[std-proposals] std::type_pack_element
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...
template <size_t I, typename ... Ts>
struct tuple_element<I, tuple<Ts...>>
{
using type = type_pack_element<I, Ts...>;
};
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAEddoJanwC6M%2B-uA00_wwOjkojTGX5CA%2BgjOb%3DGLfcGjrysPKQ%40mail.gmail.com.
Andrey Davydov
2018-10-11 19:52:22 UTC
Permalink
Post by Tony V E
Can you show an example of use?
Sent from my BlackBerry portable Babbage Device
*From: *Andrey Davydov
*Sent: *Thursday, October 11, 2018 3:05 PM
*To: *ISO C++ Standard - Future Proposals
*Subject: *[std-proposals] std::type_pack_element
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...
--
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
To view this discussion on the web visit
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/f2d44cc1-d626-4adb-9f4c-b4555ee4cb08%40isocpp.org
<https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/f2d44cc1-d626-4adb-9f4c-b4555ee4cb08%40isocpp.org?utm_medium=email&utm_source=footer>
.
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();
}
};
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/0d33adb8-2de9-4d10-9218-cdc45e950115%40isocpp.org.
Barry Revzin
2018-10-11 19:32:40 UTC
Permalink
Post by Andrey Davydov
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...
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0949r0.html

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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3790787d-92b8-46db-bcda-bd965b2178d3%40isocpp.org.
Andrey Davydov
2018-10-11 20:01:34 UTC
Permalink
Post by Barry Revzin
Post by Andrey Davydov
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...
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0949r0.html
Thank you for the link to this proposal.
Post by Barry Revzin
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.
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.
How much class instantiations does require this implementation? And what is
the total number of template arguments that will be created? I suspect that
the answer is O(N) and O(N^2) respectively, where N = sizeof...(Ts). I'd
like to have a solution requiring 0 or O(1) class instantiations.
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/2a160885-c82c-4741-8a8e-fc0fa75728a6%40isocpp.org.
Barry Revzin
2018-10-11 20:11:50 UTC
Permalink
Post by Andrey Davydov
Post by Barry Revzin
Post by Andrey Davydov
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...
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0949r0.html
Thank you for the link to this proposal.
Post by Barry Revzin
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
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.
How much class instantiations does require this implementation? And what
is the total number of template arguments that will be created? I suspect
that the answer is O(N) and O(N^2) respectively, where N = sizeof...(Ts).
I'd like to have a solution requiring 0 or O(1) class instantiations.
Boost is open-source, you can look at the
implementation: https://github.com/boostorg/mp11/blob/develop/include/boost/mp11/algorithm.hpp#L305-L320
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/d7e9f74a-b4a4-4dca-a1f0-a4bdaa1d6180%40isocpp.org.
Andrey Davydov
2018-10-11 20:17:57 UTC
Permalink
Post by Barry Revzin
Post by Andrey Davydov
Post by Barry Revzin
Post by Andrey Davydov
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...
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0949r0.html
Thank you for the link to this proposal.
Post by Barry Revzin
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
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.
How much class instantiations does require this implementation? And what
is the total number of template arguments that will be created? I suspect
that the answer is O(N) and O(N^2) respectively, where N = sizeof...(Ts).
I'd like to have a solution requiring 0 or O(1) class instantiations.
https://github.com/boostorg/mp11/blob/develop/include/boost/mp11/algorithm.hpp#L305-L320
Ah, I see, where it's available Mp11 uses builtin __type_pack_element which
I suggest to standardize.
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@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.
Bryce Adelstein Lelbach aka wash
2018-10-20 14:11:34 UTC
Permalink
This seems like a useful facility, although I wonder if language support
for random access to packs would be more elegant.

What happened to P0949? Was it reviewed?
Post by Andrey Davydov
Post by Barry Revzin
Post by Andrey Davydov
Post by Barry Revzin
Post by Andrey Davydov
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...
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0949r0.html
Thank you for the link to this proposal.
Post by Barry Revzin
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
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.
How much class instantiations does require this implementation? And what
is the total number of template arguments that will be created? I suspect
that the answer is O(N) and O(N^2) respectively, where N = sizeof...(Ts).
I'd like to have a solution requiring 0 or O(1) class instantiations.
https://github.com/boostorg/mp11/blob/develop/include/boost/mp11/algorithm.hpp#L305-L320
Ah, I see, where it's available Mp11 uses builtin __type_pack_element which
I suggest to standardize.
--
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
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
<https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/0004bbca-d6eb-4db2-8243-a5d31b80a1ba%40isocpp.org?utm_medium=email&utm_source=footer>
.
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAP3wax_%2BL%3Dj0q_YXH3ptLj_-C4n2yBb5%3D0uBkzmr77GXcqs%2BDA%40mail.gmail.com.
Matthew Woehlke
2018-10-24 16:17:53 UTC
Permalink
Post by Bryce Adelstein Lelbach aka wash
This seems like a useful facility, although I wonder if language support
for random access to packs would be more elegant.
Well, *I* sure thought so... see P0535.
Post by Bryce Adelstein Lelbach aka wash
What happened to P0949? Was it reviewed?
Hadn't seen that before. I am disappointed that it doesn't even mention
P0535.
--
Matthew
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/2cfe3cde-4fed-cea8-5c09-f91f1664b4bd%40gmail.com.
Victor Dyachenko
2018-10-25 08:16:56 UTC
Permalink
Is std::type_pack_element<2,int> just ill-formed or it's a SFINAE?
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/6152abe7-3660-45d6-81f6-de2d81e310e8%40isocpp.org.
Andrey Davydov
2018-10-25 08:41:31 UTC
Permalink
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?

четверг, 25 Пктября 2018 г., 11:16:56 UTC+3 пПльзПватель Victor Dyachenko
Post by Victor Dyachenko
Is std::type_pack_element<2,int> just ill-formed or it's a SFINAE?
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/256afdb5-3a16-4f08-b39c-cb44a1cb4c78%40isocpp.org.
Victor Dyachenko
2018-10-25 08:57:23 UTC
Permalink
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?
Can't recall at the moment... But I have analogous class in my code and 3
years ago made it SFINAE-friendly. AFAIK it gives more sensible error
messages on errors and in some cases static_asserts are just able to fire.
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/0efccfd2-ef64-4eaa-9705-ffa6bb557aec%40isocpp.org.
Louis Dionne
2018-11-01 22:43:58 UTC
Permalink
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
Post by Andrey Davydov
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...
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/114ad1ef-297f-47dc-aa22-881a9f7a702c%40isocpp.org.
c***@gmail.com
2018-11-02 05:48:26 UTC
Permalink
Post by Louis Dionne
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
Louis, I don't see how:

template < typename T, typename Alloc = allocator<T> > class vector;

could be used to index into a pack of types since there's
only one type, T, in the template. I'm guessing you mean
something else? Maybe:

template< typename[1] T > class vector;

where typename[1] is a new notation meaning maybe something
like a metaclass or maybe a type of types? And maybe
typename[2] means a type of typename[1]'s?

Could you please provide some more details of how a
std::vector could be used to solve the OP problem?

-Larry
Post by Louis Dionne
Post by Andrey Davydov
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...
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@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.
Justin Bassett
2018-11-02 15:31:03 UTC
Permalink
I believe the intent is something like `std::vector<std::type>` where
`std::type` is a value representing a type. So, instead of `template
<typename... Ts>`, you would store each T as a value in the vector.
Post by c***@gmail.com
Post by Louis Dionne
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
template < typename T, typename Alloc = allocator<T> > class vector;
could be used to index into a pack of types since there's
only one type, T, in the template. I'm guessing you mean
template< typename[1] T > class vector;
where typename[1] is a new notation meaning maybe something
like a metaclass or maybe a type of types? And maybe
typename[2] means a type of typename[1]'s?
Could you please provide some more details of how a
std::vector could be used to solve the OP problem?
-Larry
Post by Louis Dionne
Post by Andrey Davydov
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...
--
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
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
<https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/a9f1d81b-bc39-4659-ba14-42a2fa354c3d%40isocpp.org?utm_medium=email&utm_source=footer>
.
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@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.
Louis Dionne
2018-11-02 15:44:42 UTC
Permalink
Apologies for the self promotion, but this talk explains that (and more):
It may be worth it if you
are interested by the topic.

Louis
Post by Justin Bassett
I believe the intent is something like `std::vector<std::type>` where
`std::type` is a value representing a type. So, instead of `template
<typename... Ts>`, you would store each T as a value in the vector.
Post by c***@gmail.com
Post by Louis Dionne
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
template < typename T, typename Alloc = allocator<T> > class vector;
could be used to index into a pack of types since there's
only one type, T, in the template. I'm guessing you mean
template< typename[1] T > class vector;
where typename[1] is a new notation meaning maybe something
like a metaclass or maybe a type of types? And maybe
typename[2] means a type of typename[1]'s?
Could you please provide some more details of how a
std::vector could be used to solve the OP problem?
-Larry
Post by Louis Dionne
Post by Andrey Davydov
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...
--
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
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
<https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/a9f1d81b-bc39-4659-ba14-42a2fa354c3d%40isocpp.org?utm_medium=email&utm_source=footer>
.
--
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
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
<https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAPuuy5eWjTo2yPyfC1NJiFVC8_ae%2B6uVk_eD_jnxsC221ZLhMg%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAOOKoUqYnyO5kqKR-uKJnKZ-adP-Q5boxScA1GLBL%3DeRaieTbw%40mail.gmail.com.
Andrey Davydov
2018-11-02 17:54:34 UTC
Permalink
Post by Louis Dionne
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.
I believe that even when vector<metatype> will be available (after C++26?),
the following implementation of tuple_element
template<size_t I, typename...Ts>
using type_pack_element = __type_pack_element<I, Ts...>;
will be more efficient than
template<typename...Ts>
constexpr metatype metatypes[] = { $reflect(Ts)... };

template<size_t I, typename...Ts>
using type_pack_element = $unreflect(metatypes<Ts...>[i]);
In any case it is implementation detail and maintainers of standard library
can choose the most efficient way and will change it when reflection will
be supported.
--
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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/8d1ce28d-bd58-4374-b056-84ab87284f91%40isocpp.org.
Louis Dionne
2018-11-03 17:33:47 UTC
Permalink
Post by Louis Dionne
Sorry for being late in the game. I originally implemented
Post by Louis Dionne
__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.
I believe that even when vector<metatype> will be available (after
C++26?), the following implementation of tuple_element
template<size_t I, typename...Ts>
using type_pack_element = __type_pack_element<I, Ts...>;
will be more efficient than
template<typename...Ts>
constexpr metatype metatypes[] = { $reflect(Ts)... };
template<size_t I, typename...Ts>
using type_pack_element = $unreflect(metatypes<Ts...>[i]);
In any case it is implementation detail and maintainers of standard
library can choose the most efficient way and will change it when
reflection will be supported.
My point is that in most cases where you would need to index a parameter
pack today, I expect you will instead have a constexpr vector of metatypes
in the future. In that case, directly indexing in that vector is the most
straightforward way of achieving what you want. If what you have is a
parameter pack that you want to index, then yes, using __type_pack_element
would be faster and simpler, but I expect that will be rare when
metaprogramming moves into the constexpr world.

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-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAOOKoUqAADJnVi0N747VPPWF5XFcLBWJN1On9v%3DSq%3D%2B1ipum-A%40mail.gmail.com.
Continue reading on narkive:
Loading...