【发布时间】:2019-08-14 00:01:01
【问题描述】:
考虑一小段可变参数模板代码:
#include <type_traits>
template<int Dim, class... Idcs>
std::enable_if_t<sizeof...(Idcs) == 1> doit(Idcs... idcs)
{}
int main()
{
doit<0>(1);
}
当我使用 GCC/Clang 编译它时,它编译得很好,Idcs 被推断为 (int)。
但是,当我尝试使用 Intel 的编译器(版本 18.0.0,20170811)编译它时,它出于某种原因认为我手动将 Idcs 指定为空参数包,然后 enable_if_t 失败。
来自 icc 的编译器错误:
myfile.cpp(9): error: no instance of function template "doit" matches the argument list
argument types are: (int)
doit<0>(1);
^
myfile.cpp(4): note: this candidate was rejected because there is a type mismatch after argument substitution
std::enable_if_t<sizeof...(Idcs) == 1> doit(Idcs... idcs)
^
compilation aborted for myfile.cpp (code 2)
这可以通过更改 main() 内部的调用来完全指定所有模板参数来解决
doit<0, int>(1);
但是,我想了解为什么原始代码在所有 C++ 14 编译器上都没有给出相同的结果。这是预期编译成功/不成功的东西,还是某种未定义的行为,为什么?
作为参考,这些是我用来编译的命令(在 Linux 上,各种版本/风格):
g++ -std=c++14 myfile.cpp
clang++ -std=c++14 myfile.cpp
icc -std=c++14 myfile.cpp
【问题讨论】:
-
@bolev 添加,我还将第一个模板参数类型更改为
int,因为我不小心包含了一些其他不相关的标头,这些标头定义了size_t。
标签: c++ c++14 language-lawyer icc template-argument-deduction