C++ Logo

std-proposals

Advanced search

Re: fixed type parameter packs

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Wed, 12 Aug 2020 09:47:33 -0400
On Wed, Aug 12, 2020 at 7:33 AM Dominic Fandrey via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On 11/08/2020 12:52, Ville Voutilainen wrote:
> > On Tue, 11 Aug 2020 at 13:45, Dominic Fandrey via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
> >>
> >> constexpr <typename T>
> >> constexpr T min(T const & val) {
> >> return val;
> >> }
> >>
> >> template <typename T, T .. Ts>
> >> constexpr T min(T const & lhs, Ts const & ... tail) {
> >> auto const rhs = min<T>(tail ...);
> >> return lhs < rhs ? lhs : rhs;
> >> }
> >>
> >> This would allow explicitly giving a T for a whole parameter pack.
> >
> > #include <concepts>
> > #include <string_view>
> > #include <iostream>
> >
> > void f(std::convertible_to<std::string_view> auto&&... stringviews)
> > {
> > ((std::cout << stringviews << std::endl),...);
> > }
> >
> > void g(std::same_as<std::string_view> auto&... stringviews)
>

Nit: You mean `auto`, not `auto&`. Take string_views by value.


> > {
> > ((std::cout << stringviews << std::endl),...);
> > }
> >
> > int main()
> > {
> > std::string_view a("foo"),b("bar"),c("baz");
> > f(a, b, c);
> > g(a, b, c);
> > }
> >
>
> And I guess I also could:
>
> template <typename T> void f(std::convertible_to<T> auto & ... args);
>
> That does what I want, thank you!
>

Adding a layer of indirection always fixes the problem... unless the
problem is "too many layers of indirection." :)
In this case, the number of layers is observable in two and maybe three
ways:
(1) The compile-time cost.
    f("foo", "bar", "baz");
    f("a", "b", "c");
With the pack of string_views, this instantiates only one specialization of
`f`. With the pack of convertible_to<string_view, T>s, this instantiates
two distinct specializations of `f`.
(2) I'm not sure whether this one is possible: C++ has a rule that you
can't stack user-defined implicit conversions. If A is convertible to B and
B is convertible to C, that does *not* mean that A is implicitly
convertible to C (although C *would* be *explicitly* constructible from A).
So is it possible that the pack of string_views might accept some arguments
that the pack-of-Ts-convertible-to-string_view would reject?
(3) Oh, duh, of course a pack of string_views would accept
braced-initializer-list arguments, whereas a pack-of-Ts wouldn't. But
anyone who writes
    f({"abc", 2}, {}, {"foobarbaz"})
deserves what they get, in my strongly held opinion.

FWIW,
–Arthur

Received on 2020-08-12 08:51:08