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.
这篇关于如果包为空,是否对可变参数包类型执行替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果包为空,是否对可变参数包类型执行替换?


基础教程推荐
- C++,'if' 表达式中的变量声明 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17