Is substitution performed on a variadic parameter pack type if the pack is empty?(如果包为空,是否对可变参数包类型执行替换?)
问题描述
考虑以下程序:
#include <type_traits>
enum class dummy {};
template <typename T>
using EnableIf = typename std::enable_if<T::value, dummy>::type;
template <typename T>
using DisableIf = typename std::enable_if<!T::value, dummy>::type;
template <typename T>
struct dependent_true_type : std::true_type {};
template <typename T,
EnableIf<dependent_true_type<T>>...>
std::true_type f();
template <typename T,
DisableIf<dependent_true_type<T>>...>
std::false_type f();
static_assert(decltype(f<int>())::value, "");
int main() {}
GCC 4.7 高兴接受这个程序.我最近的 clang 3.1 版本声称对 f 的调用不明确.
GCC 4.7 glady accepts this program. My recent clang 3.1 build claims the call to f is ambiguous.
test.c++:22:24: fatal error: call to 'f' is ambiguous
static_assert(decltype(f<int>())::value, "");
^~~~~~
test.c++:17:16: note: candidate function [with T = int, $1 = <>]
std::true_type f();
^
test.c++:20:17: note: candidate function [with T = int, $1 = <>]
std::false_type f();
^
1 error generated.
如果我写 f<int, dummy{}>(),它确实接受程序.
It does accept the program if I write f<int, dummy{}>().
当包为空时,clang似乎没有考虑参数包的类型,这导致没有将其从候选集中删除.即使包为空,GCC 似乎也会对参数包类型执行替换,并且由于所述替换因一次重载而失败,因此没有歧义.
It seems clang does not consider the type of the parameter pack when the pack is empty, which leads to not removing it from the candidate set. GCC seems to perform substitution on the parameter pack type even if the pack is empty, and since said substitution fails for one overload, there is no ambiguity.
这两个哪个是正确的?
推荐答案
我相信我已经找到了相关的标准.§14.8.2p7 说:
I believe I have found the relevant piece of standardese. §14.8.2p7 says:
替换发生在函数类型和模板参数声明中使用的所有类型和表达式中.
The substitution occurs in all types and expressions that are used in the function type and in template parameter declarations.
由于在模板参数声明中使用了 EnableIf<dependent_true_type<T>>,因此应该进行替换,这是 clang 中的一个错误.
Since EnableIf<dependent_true_type<T>> is used in a template parameter declaration, substitution should occur and this is a bug in clang.
这篇关于如果包为空,是否对可变参数包类型执行替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果包为空,是否对可变参数包类型执行替换?
基础教程推荐
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
