考虑以下代码:
template <class T, class U, class V>
struct Foo { };
template <class T, class U>
struct Foo<T, U, std::integral_constant<int, U::value>> {
static void print()
{
std::cerr << "instantiated";
}
};
template <class U>
struct Foo<double, U, std::integral_constant<int, U::value>> {
static void print()
{
std::cerr << "instantiated special";
}
};
struct Bar {
static const int value = 0;
};
int main(int argc, char ** argv)
{
using Baz = Foo<double, Bar, std::integral_constant<int, 0>>;
Baz::print();
return 0;
}
当我使用icc 16.0.1编译时,会出现以下信息:
main.cpp(38): error: more than one partial specialization matches the template argument list of class "Foo<double, Bar, std::integral_constant<int, 0>>"
"Foo<T, U, std::integral_constant<int, U::value>>"
"Foo<double, U, std::integral_constant<int, U::value>>"
Baz::print();
使用clang 3.7.1和gcc 5.3.0可以编译此代码(并打印“实例化特殊”)。这是icc的一个错误吗,还是我的代码有误?对我来说,第二个专门化比第一个严格更加专业化;它与第一个完全相同,除了锁定第一个模板参数。
编辑:我应该补充说:如果这是icc中的错误,是否有好的解决方法?