为什么仅形成有效的空可变参差错的模板? [英] Why template with only valid empty variadic pack ill formed?

查看:63
本文介绍了为什么仅形成有效的空可变参差错的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么理由

temp.res#8.3

(8)可以在任何实例化之前检查模板的有效性. [注意:知道哪些名称是类型名称,就可以用这种方法检查每个模板的语法. —注释".如果满足以下条件,则该程序格式错误,无需进行诊断:
[..]
(8.3)可变参数模板的每个有效专业化都需要一个空的模板参数包,或者

(8) The validity of a template may be checked prior to any instantiation. [ Note: Knowing which names are type names allows the syntax of every template to be checked in this way. — end note ] The program is ill-formed, no diagnostic required, if:
[..]
(8.3) every valid specialization of a variadic template requires an empty template parameter pack, or

该规则不允许以下技巧来强制模板推导为:

That rule disallows trick as following to force template deduction as:

template <typename ...Ts,
          typename A,
          typename B,
          std::enable_if_t<sizeof...(Ts) == 0, int> = 0> // Ill-formed NDR :-(
Pair<std::decay_t<A>, std::decay_t<B>> MakePair(A&&a, B&& b)
{
    return {a, b};
}

注意:我知道C ++ 17演绎规则会使make_*过时.

Note: I know that C++17 deduction rule makes make_* obsolete.

推荐答案

通常,由于您始终无法生成有效的实例化,因此实现可以尽早诊断出模板中的明显错误.但是pack扩展有点独特,因为整个构造在实例化时就可以消失.因此,需要该规则以禁止在包装扩展的事物中出现各种明显的bossity,并允许对其进行早期诊断:

In general, implementations can diagnose obvious errors in templates early, since you can't generate a valid instantiation anyway. But pack expansion is a bit unique because the whole construct can just disappear when instantiated. The rule therefore is needed to disallow various species of obvious bogosity in things being pack-expanded and permit their early diagnosis:

template<class...Ts>
union U : Ts... {}; // unions can't have base classes

template<class...Ts>
class C : Ts..., Ts... {}; // classes can't have duplicate direct base classes

template<class...Ts>
void f() {
    // sizeof(void) is invalid, but it can vanish
    int x[] = {(sizeof(void) + sizeof(Ts))..., 0};
}

这在某种程度上也对编译器实现者有所帮助,因为它们的内部模板表示不需要支持这种废话.

This also somewhat helps compiler implementers, because their internal representation for templates need not support such nonsense.

这篇关于为什么仅形成有效的空可变参差错的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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