【发布时间】:2015-03-31 08:00:34
【问题描述】:
我正在尝试在我的一个项目中添加对 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++ 和 clang++ 来说就像一个魅力,但无法用 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。
谢谢
【问题讨论】:
-
显而易见的解决方法是部分专业化。即使这意味着将
apply移动到impl_base<Selector>,并将using impl_base<Selector>添加到impl本身。这完全回避了对 SFINAE 的需求。 -
您使用的是哪个版本的 ICC?
-
这是格式错误的,无论如何都不需要诊断。对于
impl的任何给定实例化,三个applys 中的至少两个都不能生成有效的特化。 -
你想要完成什么?
-
我使用的是 icc 16.0.2.164。我只是想根据父类的类型参数之一选择一个函数。 @T.C.你能发展吗?就我而言,该类中还有其他模板参数,并且我在项目中的几个地方都遇到了这个问题,我想避免更改太多代码。