Topic: std::select_t<I, Ts...> returns the I'th T from Ts...
Author: Marc Mutz <marc.mutz@kdab.com>
Date: Tue, 24 Oct 2017 10:49:06 +0200
Raw View
Hi,
I have found myself implementing a lot of tuple access protocols lately,
and while tuple_size and get()[1] can, in general, be handled by just
one 'overload', I have not found an easy way for tuple_element. I thus
implemented a small helper locally, so I can say:
template <size_t I>
struct tuple_element<I, MyClass> : select<I, std::string, double,
int> {};
iow: select<I, Ts...> picks the I'th T (zero-based) from pack Ts...
Possible implementation:
template <size_t I, typename Head, typename...Tail>
struct select : select<I-1, Tail> {};
template <typename Head, typename...Tail>
struct select<0, Head, Tail...> { using type = Head; };
I haven't found anything existing on this mailing-list or in C++17, so
I'm wondering whether I should propose it.
I'm a bit concerned about the name. 'select' is used for the Posix
function already, and 'switch' is a keyword. 'selection' doesn't really
work, either. Or something entirely different like pick<I>::from<Ts...>?
What do you think?
Thanks,
Marc
[1] The four get() overloads can be generated with perfect forwarding
and constexpr-if:
class QPoint {
int x, y;
// ...
template <size_t I, typename Point>
requires std::is_same_v<std::decay_t<Point>, QPoint>>
friend constexpr decltype(auto) get(Point &&p) noexcept {
if constexpr (I == 0) {
return (std::forward<Point>(p).x);
else if constexpr (I == 1) {
return (std::forward<Point>(p).y);
}
};
(at least I have not found a way this breaks on my use-cases, but I also
admit that I have not gone to the length of proving it correct to
myself).
--
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+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/851fb43e541107ecafde9e00efe3bf64%40kdab.com.
.
Author: Marc Mutz <marc.mutz@kdab.com>
Date: Tue, 24 Oct 2017 11:12:27 +0200
Raw View
On 2017-10-24 10:49, Marc Mutz wrote:
> I'm a bit concerned about the name. 'select' is used for the Posix
> function already, and 'switch' is a keyword. 'selection' doesn't
> really work, either. Or something entirely different like
> pick<I>::from<Ts...>?
Boost.MPL uses at<>, but that doesn't work on pack expansions directly:
mpl::at<mpl::vector<Ts...>, I>
One could also just use
tuple_element<I, MyClass> : tuple_element<I, std::tuple<int,
std::string, double>> {};
but there's always the nagging feeling that some of tuple's magic will
change the result here...
--
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+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/31ecee6e705799e1df3cd2170011b13b%40kdab.com.
.