我有一个关于从通过模板继承的类中提取 typedef 信息的问题。为了说明我的问题,请考虑以下简单示例:
#include <iostream>
class A1{
public:
void print(){ printf("I am A1\n"); };
};
class A2{
public:
void print(){ printf("I am A2\n"); };
};
class B1{
public:
typedef A1 A;
};
class B2{
public:
typedef A2 A;
};
template<class b>
class C{
typedef class b::A AA;
AA a;
public:
void Cprint() { a.print(); };
};
int main()
{
C<B1> c1;
c1.Cprint();
C<B2> c2;
c2.Cprint();
}
C 类将一个类(B1 或 B2)作为模板参数。B1 和 B2 都有一个名为 A 的 tyepdef(分别是 A1 和 A2 类)。在编译时,C 类应该能够确定“B”类正在使用两个“A”类中的哪一个。当我用 g++ 编译时,上面的代码运行良好。但是,当我使用 Intel 的 icpc 编译它时,出现以下错误:
test.cpp(24): error: typedef "A" may not be used in an elaborated type specifier
typedef class b::A AA;
^
detected during instantiation of class "C<b> [with b=B1]" at line 33
有没有其他方法可以达到类似的效果?当然,我的实际代码要复杂得多,我希望以这种方式构造类是有原因的。还有一些原因我想用 icpc 而不是 g++ 编译。
提前致谢。卡尔