Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we have variadic concepts before variadic template parameters?

Can we have variadic concepts before variadic template parameters?

I meant, is following legal:

template <class, std::size_t> concept Any = true;

template <class> struct n_ary;

template <std::size_t... Is>
struct n_ary<std::index_sequence<Is...>>
{
  template <Any<Is>... Ls, typename ... Ts>
  void operator()(Ls..., Ts...) {}
};

Demo (accepted only by clang)

Note: Without the extra Ts, it is accepted by all compilers Demo.

like image 556
Jarod42 Avatar asked May 31 '26 04:05

Jarod42


1 Answers

This is probably in violation of [temp.param] section 14:

... A template parameter pack of a function template shall not be followed by another template parameter unless that template parameter can be deduced from the parameter-type-list ([dcl.fct]) of the function template or has a default argument ([temp.deduct]). ...

It includes this example:

template<class T1 = int, class T2> class B;     // error

// U can be neither deduced from the parameter-type-list nor specified
template<class... T, class... U> void f() { }   // error
template<class... T, class U> void g() { }      // error

However, cases like f(T..., U...) are currently accepted by compilers, which see one (or both) parameter packs as empty.

By the the example in section 5 about type constraints,

template<C3<int>... T> struct s5;       // associates (C3<T, int> && ...)

the syntax in your example should be "functionally equivalent, but not equivalent" to template<typename... Ls, typename... Ts> requires (Any<Ls,Is> && ...). The expansion rules [temp.variadic] require that Ls and Is have the same size, which is why gcc and MSVC ultimately reject the call in your example. Interestingly, if you use the functionally equivalent syntax, clang does join the other compilers in rejecting it.

like image 88
sigma Avatar answered Jun 02 '26 18:06

sigma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!