C++ Logo

std-proposals

Advanced search

Re: fixed type parameter packs

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Tue, 11 Aug 2020 13:52:48 +0300
On Tue, 11 Aug 2020 at 13:45, Dominic Fandrey via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> There is another feature I've been wondering why it doesn't exist.
> I'll show this by example:
>
> template <std::string_view ... Ts>
> constexpr std::string concat(Ts ... strs) {
> using std::cend;
> using std::cbegin;
> std::string result;
> result.reserve(strs.size() + ...);
> return (result.insert(cend(result), cbegin(strs), cend(strs)), ...);
> }
>
> An alternative use case:
>
> 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)
{
    ((std::cout << stringviews << std::endl),...);
}

int main()
{
    std::string_view a("foo"),b("bar"),c("baz");
    f(a, b, c);
    g(a, b, c);
}

Received on 2020-08-11 05:56:23