Search code examples
c++templatesenumslanguage-features

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. 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?

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 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> that implicitly says what type of classes can be used as template parameter. Can it be done in C++ using enums? How to work with enumed types in statements?

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.


Solution

  • The type traits idiom as @UncleBens illustrates is the usual way of solving this problem.

    You can attach information to classes using static const members of integer or enumeration type, as well.

    #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 >();
    }