我希望将一组用 C++ 编写的库与英特尔编译器一起使用。我已附上演示该问题的示例代码。库中有很多地方将“using”指令与部分重载结合起来(例如,我想使用基类中的 foo(void) 方法,但在派生类中重新实现第二个版本 fo foo ) 。 gcc 没有问题,但 intel 有问题。
#include <iostream>
template <class F>
struct Interface
{
static const F f=10;
};
template <class F>
struct Base : public Interface<F>
{
void foo (void) { std::cout << "void" << std::endl; }
template <class FF>
void foo (Interface<FF> &ii) { std::cout << "F : " << ii.f << std::endl; }
};
template <class F,int i>
struct Derived : public Base<F>
{
// void foo (void) { Base<F>::foo(); } // works fine
using Base<F>::foo; // gives error
template <class FF>
void foo (Interface<FF> &ii) { std::cout << "Derived<" << i << "> F : " << ii.f << std::endl; }
};
int main (void)
{
Derived<double,10> o;
o.foo(); // ok
o.foo (o); // problem
}
icc给出的编译错误是:
test.cc(30): error: more than one instance of overloaded function "Derived<F, i>::foo [with F=double, i=10]" matches the argument list:
function template "void Base<F>::foo(Interface<FF> &) [with F=double]"
function template "void Derived<F, i>::foo(Interface<FF> &) [with F=double, i=10]"
argument types are: (Derived<double, 10>)
object type is: Derived<double, 10>
o.foo (o); // problem
^
compilation aborted for test.cc (code 2)
如果你删除线
using Base<F>::foo;
并将其替换为行
void foo (void) { Base<F>::foo(); }
一切正常。
我的问题是有人知道这是一个特殊的 gcc 功能还是 icc 错误吗?或者是否有另一种不涉及更改代码的解决方法?
这是使用 g++.real (Ubuntu 4.4.3-4ubuntu5) 4.4.3 和 icc (ICC) 12.0.2 20110112。
对于C++11,相关标准引用可以在
找到7.3.3 using 声明 [namespace.udecl]
14/ 如果命名空间作用域或块作用域中的函数声明与 using 声明引入的函数具有相同的名称和相同的参数类型,并且这些声明没有声明相同的函数,则程序格式错误.
这支持基于 EDG 的编译器。然而,有一种特殊情况是在课堂上使用的:
15/ 当 using 声明将基类中的名称带入派生类作用域时,派生类中的成员函数和成员函数模板将覆盖和/或隐藏具有相同名称、参数的成员函数和成员函数模板基类中的 type-list (8.3.5)、cv-qualification 和 ref-qualifier(如果有)(而不是冲突)。 [ 注意:有关命名构造函数的 using 声明,请参阅 12.9。 -结尾 注意] [示例:
struct B {
virtual void f(int);
virtual void f(char);
void g(int);
void h(int);
};
struct D : B {
using B::f;
void f(int); // OK: D::f(int) overrides B::f(int);
using B::g;
void g(char); // OK
using B::h;
void h(int); // OK: D::h(int) hides B::h(int)
};
void k(D* p)
{
p->f(1); // calls D::f(int)
p->f(’a’); // calls B::f(char)
p->g(1); // calls B::g(int)
p->g(’a’); // calls D::g(char)
}
—结束示例]
因此,在C++11中,Comeau和Intel似乎都是错误的。我不知道这些规则是否同样适用于C++03