参数列表中的可变参数模板,参数包及其讨论的歧义 [英] Variadic templates, parameter pack and its discussed ambiguity in a parameter list

查看:79
本文介绍了参数列表中的可变参数模板,参数包及其讨论的歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此问题中,我将参考我之前的问题

In this question, I'll refer to my previous question.

在该问题中,我发现以下内容无效:

In that question, I found that the following is not valid:

template<typename T, typename... A, typename S>
class C { };

这是因为:


[这是无效的代码]对于类模板,因为必须始终指定它们的参数,除非参数包位于末尾并混淆所有剩余的模板参数,否则始终会导致模棱两可。

[It is not valid code] for class templates because their arguments must always be specified, which will always result in an ambiguity unless the parameter pack is at the end and slurps up any remaining template parameters.

当然这是有道理的。

然后,作为替代方案

template<typename F, typename S>
class C;

template<typename T, typename... A, typename S>
class C<T(A...), S> { };

实际上,它似乎可以工作,因此多亏提出该建议的人。

Actually, it seems to work, so thanks to the one that proposed it.

无论如何,我不明白的是为什么这是有效的代码,而前一个却不是。

它是否应该遭受与前一个解决方案相同的歧义?在这种情况下,编译器为什么以及如何解决这种歧义?

根据前面的问题(请参阅本问题开头的链接),在我看来,可变参数部分仍应保留

当然,我错了,但是我的推理到底出了什么问题?

Anyway, what I don't understand is why this is valid code while the previous one was not.
Should it suffer from the same ambiguity of the previous solution? Why and how does the compiler solve that ambiguity in that case?
According with the previous question (see the link at the start of this question), it seems to me that still the variadic part should slurp up any parameters to the end, thus this code should not be valid as well.
I'm wrong, of course, but what's wrong exactly in my reasoning?

推荐答案

在类模板中,预期模板参数列表 C< a,b,c,d,e,f> 需要匹配

In the class template, a prospective template argument list C<a,b,c,d,e,f> needs to match

template<typename T, typename... A, typename S>

其中... A只是浮在参数列表中。

in which ...A is just floating in a parameter list.

但是在专业化中,需要匹配的不是模板变量列表,而是模式:

But in the specialization, what needs to be matched is not the list of template variabled but rather the pattern:

C<T(A...), S>

这很容易,因为 A ... >是定界的。

which is easy because the A... is delimited.

因此在模板专业化中,参数列表只是符号清单,一些标量和一些参数包,它们将以模式显示。

So in the template specialization, the list of parameters is just an inventory of symbols, some scalar and some parameter packs, which will appear in a pattern.

这篇关于参数列表中的可变参数模板,参数包及其讨论的歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆