参数化类的别名(或类型定义)内部类
Aliasing (or typedef'ing) inner class of parameterized class
假设我有这个(使用C++03(。
template <class T, int S>
class FiniteMap
{
public:
class Iterator {};
class Entry {};
};
class Foo {};
template <class T, int S>
class FooMap : public FiniteMap<T,S>
{
public:
void bar()
{
FooMap<T,S>::Iterator iter;
FooMap<T,S>::Entry entry;
}
};
int main()
{
return 0;
}
我想typedef
FooMap<T,S>::Iterator
和FooMap<T,S>::Entry
,但如果我尝试这个:
typedef FooMap<T,S>::Iterator FooIterator;
我得到"错误:未在此范围中声明'T'"。如果我试着把模板参数放在上面:
typedef
template <class T, int S>
FooMap<T,S>::Iterator FooIterator;
我得到"错误:在'template'之前应为非限定id">
我使用了#define:
#define FooIterator typename FooMap<T,S>::Iterator
这似乎是有效的(尽管它不适用于在线C++编译器(。
看起来有点生气。
有什么想法吗?
C++11具有`using for this:(
当我在C++03上尝试时,我得到了错误"在FiniteMap
之前需要typename
,因为它是一个依赖范围…
因此:
template <class T, int S>
class FiniteMap {
public:
class Iterator {};
class Entry {};
};
class Foo {};
template <class T, int S>
class FooMap : public FiniteMap<T, S> {
public:
typedef typename FiniteMap<T, S>::Iterator FooIterator;
typedef typename FiniteMap<T, S>::Entry FooEntry;
void bar()
{
FooIterator iter;
FooEntry entry;
}
};
int main()
{
FooMap<int, 3> test;
return 0;
}
GodBolt
使用
typedef FooMap<T,S>::Iterator FooIterator;
编译器抱怨T(可能还有S(并没有被声明——我不得不同意
如果你想保持T的抽象性,那么你必须使用参数化模板。
但我认为你实际上想为T=Foo和S=5的特定情况键入一个"short"。
这可以通过来完成
/* ... start of your file */
class Foo {};
typedef FooMap<Foo,5>::Iterator FooIterator;
int main()
{
}
相关文章:
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 列表参数的类型定义
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 关于 C++ 中的函数类型定义
- C++(和 ROS) - 包含与前向声明引用,设置默认值和类型定义
- 将使用/类型定义限制为类范围
- 模板类型定义?
- C++:模板类的类型定义
- 如何对命名空间限定类型进行类型定义?
- 此递归模板类型定义是否有效C++?
- 具有调整对齐方式的类型定义
- C++从抽象类型定义类成员
- 用于C++代码的 API 监视器类型定义 (XML)
- 如何将result_of与函数类型定义一起使用
- 在C++的适当类型定义位置
- 如何根据模板类型定义浮点常量?
- 如何为缺少预定义运算符而不扩展命名空间"std"的标准类型定义运算符>> (istream &, ...)?
- 参数化类的别名(或类型定义)内部类
- 如果我想从类型"T"定义元素的容器(来自 STL),那么"T"必须使用默认构造函数?