我想在我的一个项目中添加对icc的支持,但是当有两种以上的方法时,我有一些SFINAE的问题。这是问题的一个裸简单的例子:如何使用英特尔C++编译器(ICC)的三种方法使用SFINAE?
#include <iostream>
template<std::size_t Selector>
struct impl {
template<bool Enable = true, typename std::enable_if<Selector == 1 && Enable, int>::type = 0>
static void apply(){
std::cout << "First selector" << std::endl;
}
template<bool Enable = true, typename std::enable_if<Selector == 2 && Enable, int>::type = 0>
static void apply(){
std::cout << "Second selector" << std::endl;
}
template<bool Enable = true, typename std::enable_if<Selector == 3 && Enable, int>::type = 0>
static void apply(){
std::cout << "Big selector" << std::endl;
}
};
int main(){
impl<1>::apply();
impl<2>::apply();
impl<3>::apply();
return 0;
}
这种工作方式和g的魅力++和铛++,但无法与ICC编译:
test.cpp(16): error: invalid redeclaration of member function template "void impl<Selector>::apply() [with Selector=1UL]" (declared at line 11)
static void apply(){
^
detected during instantiation of class "impl<Selector> [with Selector=1UL]" at line 22
test.cpp(11): error: invalid redeclaration of member function template "void impl<Selector>::apply() [with Selector=3UL]" (declared at line 6)
static void apply(){
^
detected during instantiation of class "impl<Selector> [with Selector=3UL]" at line 24
compilation aborted for test.cpp (code 2)
是否有此与ICC解决方法?我想避免更改太多的代码,我在我的项目的几个地方有这个问题。
我正在使用icc 16.0.2.164。
感谢
显而易见的解决方法是部分专业化。即使这意味着将“应用”到“impl_base”,并将“使用impl_base ”添加到“impl”本身。这完全避免了对SFINAE的需求。 –
MSalters
2015-03-31 08:05:04
你使用什么版本的ICC? – usr1234567 2015-03-31 08:15:44
这是不合格的,无论如何不需要诊断。对于任何给定的'impl'的实例,对于至少两个“apply”中的至少两个,不能生成有效的特化。 – 2015-03-31 08:15:57