Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 220
Adrian Avatar asked Dec 09 '17 17:12

Adrian


People also ask

What is typename in template?

" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.

What is the difference between template typename T and template T?

There is no difference. typename and class are interchangeable in the declaration of a type template parameter.

What is the difference between typename and class in template?

There is no difference between using <typename T> OR <class T> ; i.e. it is a convention used by C++ programmers. I myself prefer <typename T> as it more clearly describes its use; i.e. defining a template with a specific type.

What is the validity of template parameters?

What is the validity of template parameters? Explanation: Template parameters are valid inside a block only i.e. they have block scope.


1 Answers

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.

like image 175
Barry Avatar answered Sep 24 '22 00:09

Barry