如何从通过模板继承的类中提取 typedef'd 信息?
How can you extract typedef'd information from a class that is inherited through templates?
我有一个关于从通过模板继承的类中提取typedef'd信息的问题。为了说明我的问题,请考虑以下简单示例:
#include <iostream>
class A1{
public:
void print(){ printf("I am A1n"); };
};
class A2{
public:
void print(){ printf("I am A2n"); };
};
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(分别为 A1 和 A2 类)的 tyepdef。在编译时,类 C 应该能够找出"B"类正在使用两个"A"类中的哪一个。当我使用 g++ 编译时,上面的代码可以完美运行。但是,当我使用英特尔的 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++ 编译也是有原因的。
提前谢谢。卡尔
尝试更改:
template<class b>
class C{
typedef class b::A AA;
AA a;
public:
void Cprint() { a.print(); };
};
自:
template<class b>
class C{
typedef typename b::A AA;
AA a;
public:
void Cprint() { a.print(); };
};
b::A
是依赖类型(它取决于用于b
的类型)。
仅供参考,原始发布的代码无法使用VS2008和VS2010进行编译。
相关文章:
- C++:TypeDef使用元组
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么istream不支持右值提取
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 如何设置一个范围来提取我想要获得的信息
- 为什么在使用typedef时类推导指南会失败
- 视觉工作室项目.提取源文件夹名称
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 尝试根据类中 typedef 的存在来专门化模板函数
- 如何重新定义MPI_FLOAT,MPI_DOUBLE以 typedef 的方式
- 从字符串中提取整数并形成一个数组
- typedef 枚举和枚举类有什么区别?
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 从 std::vector<无符号字符>切片中提取 int?
- 在C++中使用重载提取运算符时出现问题
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 如何从通过模板继承的类中提取 typedef'd 信息?