Adrian
Adrian

Reputation: 10911

Is not matching a template<typename...> to template<typename> a defect?

While exploring this answer I discovered that a template that takes a parameter pack will not be accepted by a template that expects template that has a specific number of parameters.

This seems to me that it is a defect since if a template can take any number of parameters, it should be able to map to a specific number. Is there a language lawyer that could explain why this is not allowed?

Here is a simple example:

template <typename...Ts>
using pack = void;

template <template <typename> class>
using accept_template = int;

accept_template<pack> value = 0;

I wouldn't use it in this exact scenario of course. It would be used to pass a template to another template which would use the passed template in some manner. In my answer that I linked, I have stated a workaround, but I still feel that this is a defect.

Upvotes: 8

Views: 335

Answers (1)

Barry
Barry

Reputation: 303566

This restriction was loosened as a result of P0522, which introduces new rules to handle how template template-arguments match template template-parameters. As a result, from the paper:

template<class T, class U = T> class B { /* ... */ };
template <class ... Types> class C { /* ... */ };
template<template<class> class P> class X { /* ... */ };


X<B> xb; // OK, was ill-formed: 
         // default arguments for the parameters of a template argument are ignored

X<C> xc; // OK, was ill-formed: 
         // a template parameter pack does not match a template parameter

Your example fails to compile in C++14, but will compile in C++17.

Upvotes: 12

Related Questions