views:

110

answers:

2

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

I suggest organize your answers presenting some theory first, explaining your idea, and support it by an example.

Thank you for your participation!

+4  A: 

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.

UncleBens
+1  A: 

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

You can attach information to classes using static const int members, as well. Although it is nonstandard, both GCC and Comeau (which is known for trying not to extend the language) allow static const enum as an extension.

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