考慮下面的代碼:模板偏特和ICC
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();
鏗鏘3.7.1和gcc 5.3.0編譯(和「實例化特殊」打印)。這是icc中的錯誤,還是我的代碼不正確?對我來說,似乎很清楚,第二個專業比第一個專業嚴格得多。它與第一個完全相同,而不是鎖定第一個模板參數。
編輯:我應該補充:如果這是icc中的錯誤,是否有一個很好的解決方法?
當然看起來像icc中的一個bug給我。 – Cameron
您能否檢查[下面的評論](https://stackoverflow.com/questions/37216212/partial-template-specialization-and-icc#comment61972971_37216503)中的建議是否解決了ICC上的問題?當然,對於您的問題不是一個完整的解決方案,只是向前邁進了一步。 – bogdan
在實際的代碼中,整數來自U的嵌套成員,這反過來又給了叮噹問題,所以我的同事從非類型轉換爲類型模板參數。 –