如何定义用枚举模板的容器
how to define container templated with enum
我需要创建一个容器,该容器可容纳整数并用枚举模板。
enum Color{R,G,B};
template<class Color C,//class before color will be removed
template <class, class = allocator<int>> class Container>
class MyClass
{
Container<int> buffer;
}
我需要创建向量并将其列出。类似:
MyClass<Color::R, std::list> mbs
MyClass<Color::G, std::vector> mbs
//wrong number of template arguments (1, should be 2)
MyClass<Color C, vector> v1;
//for contaner
Container<int>::iterator nth = buffer.begin()
您正在寻找非类型模板参数
对于您的容器,它将像这样定义:
template<Color C,
template<class, class = std::allocator<int>> class Container>
class MyClass{
Container<int> buffer;
// ...
};
您可以创建一个类似的实例:
MyClass<Color::R, std::vector> instance;
您可以创建这样的迭代器:
typename Container<int>::iterator iter = buffer.begin();
typename
是必需的,因为iterator
是依赖的名称
相关文章:
- 如何正确实现和访问运算符的各种自定义枚举器
- 枚举类'classname'的多重定义
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- C++:如何在声明枚举类的模板类主体之外定义枚举类?
- C2365 重新定义 VS2019 升级后,以前的定义是外部标头 + lib 的"枚举器"
- 如何在 std::p air 中使用 System::Guid 和自定义枚举?
- 如何理解c++中在命名空间内部定义的枚举类型enum
- 当在基类中定义枚举时,qml中的Q_ENUM未定义值
- 在模板类内部定义的枚举上的嵌套类的部分专用化
- 枚举的复合赋值运算符真的应该根据其关联的算术运算符来定义吗?
- 如何检查类中定义的枚举类型变量是否被分配给?
- 在 FlatBuffer 中使用自定义枚举
- 为什么方法重载或枚举标志定义会触发 gcc7.2 编译器警告?
- 新的类型安全枚举是否定义为从 0 开始
- 无法获取库上的枚举(未定义的引用静态元对象)
- 枚举元素定义
- 为类模板的枚举成员定义 std::hash
- 如何定义自定义值的类型?(枚举类型定义)
- 我怎么能有一个枚举自定义类
- 使用(非类型)枚举参数定义内部类成员函数模板