在专业化的情况下,检查过的防护参数包是否会导致程序格式错误? [英] Are checked guard parameter packs cause of ill-formed programs in case of specializations?

查看:86
本文介绍了在专业化的情况下,检查过的防护参数包是否会导致程序格式错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此<的后续内容问题.

考虑以下代码:

#include <type_traits>

template<typename T, typename... P, typename U = std::enable_if_t<std::is_integral<T>::value>>
void f() { static_assert(sizeof...(P) == 0, "!"); }

int main() {
    f<int>();
}

它可以编译,但是根据 [temp.res]/8 格式错误,由于以下原因,无需进行诊断:

It compiles, but according to [temp.res]/8 it is ill-formed, no diagnostic required because of:

可变参数模板的每个有效专业化都需要一个空的模板参数包

every valid specialization of a variadic template requires an empty template parameter pack

现在考虑这个稍微不同的示例:

Now consider this slightly different example:

#include <type_traits>

template<typename T, typename... P, typename U = std::enable_if_t<std::is_integral<T>::value>>
void f() { static_assert(sizeof...(P) == 0, "!"); }

template<>
void f<int, int>() { }

int main() {
    f<int, int>();
}

在这种情况下,存在有效的完全显式专门化,其参数包不为空.
这足以说明代码不再格式错误了吗?

In this case a valid full explicit specialization exists for which the parameter pack is not empty.
Does this suffice to say that the code is no longer ill-formed?

注意:我不是在寻找其他方法,例如将std::enable_if_t放在返回类型或类似类型中.

Note: I'm not looking for alternative ways like putting the std::enable_if_t in the return type or similar.

推荐答案

[temp.res]/8讨论的是模板声明,而不是实体.也就是说,它分别讨论了主要模板和部分专业化 ;这些模板"必须每个具有有效的专业化规则.否则,必须以相同的方式解释该段落中的第一个项目符号,这绝对不会赋予其预期的含义.

[temp.res]/8 talks about template-declarations, not the entity. That is, it talks about primary templates and partial specializations individually; these "templates" must each have a valid specialization subject to the rules. Otherwise, the first bullet in that paragraph would have to be interpreted in the same manner, which definitely doesn't give it its intended meaning.

template <typename T>
void f() {T+0;} // wouldn't be allowed to diagnose this, because there could be an 
                // explicit specialization that doesn't contain this statement...?

这篇关于在专业化的情况下,检查过的防护参数包是否会导致程序格式错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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