简体   繁体   English

如何在C ++中将模板与枚举结合起来?

[英]How to combine templates with enums in C++?

There are a huge feature collection in C++ programming language that supply a strict control over datatypes. C ++编程语言中有一个巨大的特性集合,可以严格控制数据类型。 Frequently one would mold his code with template system to achieve the most adequate functionality while guaranteeing within it correct type preservation and flexibility in its manipulation. 通常,人们会使用模板系统来模拟他的代码,以实现最合适的功能,同时保证其内部的正确类型保存和操作灵活性。 Less frequently enumerations are used for this purpose, since they permit define an explicit possibilities for the data and have no direct way to verify type correctness. 较少使用的枚举用于此目的,因为它们允许为数据定义明确的可能性,并且没有直接的方法来验证类型的正确性。

So what we have are templates that allow functions and classes to operate with generic types without being rewritten for each one; 所以我们拥有的模板允许函数和类使用泛型类型而不需要为每个类型重写; and enums, providing a straight use of the expected types. 和枚举,直接使用预期类型。

Is it possible to define a restricted template architecture for a class using an enum as a model? 是否可以使用枚举作为模型为类定义受限制的模板架构? How can a templatized method use enumed type and when it could be useful? 模板化方法如何使用enumed类型以及什么时候有用呢?

My final question is: how to conjoin templates and enumeration types and use them together in the abstract data design? 我的最后一个问题是:如何结合模板和枚举类型并在抽象数据设计中一起使用它们?

As an example suppose you are trying to create a data container you expect to have types defined in an enum within your class: 例如,假设您正在尝试创建数据容器,您希望在类中的枚举中定义类型:

template <typename T>
class element
{
public:
  enum type { text_type, widget_type, image_type };

  ...
private:
  type node_type;

};

You also defined each of the types present in class element : 您还定义了class element每个类型:

class text;
class widget;
class image;

Creating an element, you want to specify its content type (text, widget or image) so you pass it by template argument. 要创建元素,您需要指定其内容类型(文本,小部件或图像),以便通过模板参数传递它。

if (node_type == text_type) do_something();

In Java you have List<? extends T> 在Java中你有List<? extends T> List<? extends T> that implicitly says what type of classes can be used as template parameter. List<? extends T>隐含地说明什么类型的类可以用作模板参数。 Can it be done in C++ using enums? 可以使用枚举在C ++中完成吗? How to work with enumed types in statements? 如何在语句中使用enumed类型?

Question : I'd like to know how to restrict the behavior of templates by enumed types to ensure only these are being used, and orient the code using traditional way of working with enums. 问题 :我想知道如何通过枚举类型来限制模板的行为,以确保只使用这些模板,并使用传统的枚举方式来定位代码。

If you don't need to use enums, I believe you could employ similar techniques used in the standard library for categorizing iterators. 如果您不需要使用枚举,我相信您可以使用标准库中使用的类似技术来对迭代器进行分类。

class text {};
class widget {};
class image {};

struct text_type_tag {};
struct widget_type_tag {};
struct image_type_tag {};

template <class T>
struct element_traits;

template <>
struct element_traits<text>
{
    typedef text_type_tag category;
};

template <>
struct element_traits<widget>
{
    typedef widget_type_tag category;
};

template <>
struct element_traits<image>
{
    typedef image_type_tag category;
};

//add specializations for any other type you want to categorize

//a template that only works with widget types
template <class Widget>
void foo_implementation(Widget w, widget_type_tag);

template <class Widget>
void foo(Widget w)
{
    foo_implementation(w, typename element_traits<Widget>::category());
}

int main()
{
    foo(widget());
    foo(10);  //error, element_traits not specialized for int (incomplete)
    foo(image()); //error, no matching call for foo_implementation(image, image_type_tag);
}

A further possibility with this: overloading foo_implementation for other categories of elements. 另一种可能性是:为其他类别的元素重载foo_implementation

The type traits idiom as @UncleBens illustrates is the usual way of solving this problem. @UncleBens说明的类型特征成语是解决此问题的常用方法。

You can attach information to classes using static const members of integer or enumeration type, as well. 您也可以使用整数或枚举类型的static const成员将信息附加到类。

#include <iostream>

enum color { red, green, blue };

struct x {
    static const color c = red;
};

template< color c >
struct thing;

template<>
struct thing< red > {
    thing() { std::cout << "red\n"; }
};

int main() {
    thing< x::c >();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM