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

天大地大妈咪最大 提交于 2019-12-10 12:49:44

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/47731088/is-not-matching-a-templatetypename-to-templatetypename-a-defect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!