首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >匹配template<typename...>与template<typename>不是缺陷吗?

匹配template<typename...>与template<typename>不是缺陷吗?
EN

Stack Overflow用户
提问于 2017-12-09 17:16:08
回答 1查看 329关注 0票数 8

在研究这个答案时,我发现使用参数包的模板不会被期望有特定数量参数的模板所接受。

在我看来,这是一个缺陷,因为如果一个模板可以接受任意数量的参数,它应该能够映射到一个特定的数字。有没有语言律师能解释为什么不允许这样做?

下面是一个简单的例子:

代码语言:javascript
复制
template <typename...Ts>
using pack = void;

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

accept_template<pack> value = 0;

当然,我不会在这种情况下使用它。它将用于将模板传递给另一个模板,该模板将以某种方式使用传递的模板。在我联系的答案中,我提出了一个解决办法,但我仍然觉得这是一个缺陷。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-09 17:52:05

由于P0522引入了新的规则来处理模板参数匹配模板参数的问题,这种限制被放松了。因此,从文件中:

代码语言:javascript
复制
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

您的示例无法在C++14中编译,但将在C++17中编译。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47731088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档