Re: How to create an array of data types only

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 05/13/04


Date: Thu, 13 May 2004 06:50:59 +0100


"billy" <billy_dev#@#cox#.#net> wrote in message
news:zDDoc.4148$2c7.587@fed1read07...
> I've got a set of subclasses that each derive from a common base class.
What
> I'd
> like to do is create a global array of the class types (or, class names)
> that a manager
> class can walk through in its constructor and instantiate one of each of
the
> class types
> in this global array. So, it's almost like the global array has to hold
data
> types as
> opposed to data.

You cannot create an array of types, but you can create a list of types.
Here's some code to illustrate the idea.

In the code below MyTypeList is the list of types and I recursively loop
through it using the print_type_names template function.

#include <iostream>
using namespace std;

template <class T, class U>
struct TypeList
{
 typedef T Head;
 typedef U Tail;
};

struct NullType
{
};

#define TYPELIST_0() NullType
#define TYPELIST_1(A) TypeList<A, TYPELIST_0() >
#define TYPELIST_2(A, B) TypeList<A, TYPELIST_1(B) >
#define TYPELIST_3(A, B, C) TypeList<A, TYPELIST_2(B, C) >
#define TYPELIST_4(A, B, C, D) TypeList<A, TYPELIST_3(B, C, D) >
#define TYPELIST_5(A, B, C, D, E) TypeList<A, TYPELIST_4(B, C, D, E) >

struct SomeType {};
struct AnotherType {};
struct YetAnotherType {};

typedef TYPELIST_5(int, SomeType, double, AnotherType, YetAnotherType)
MyTypeList;

template <class TL>
void print_type_names()
{
 cout << typeid(TL::Head).name() << '\n';
 print_type_names<TL::Tail>();
}

template <>
void print_type_names<NullType>()
{
}

int main()
{
 print_type_names<MyTypeList>();
}

On my computer this prints

int
SomeType
double
AnotherType
YetAnotherType

yours may differ because the output of typeid(...).name() is implementation
dependent.

This type list idea was popularised by Alexei Alexandrescu in his book
Modern C++ Design, if you are interested.

john



Relevant Pages

  • Cleaning up FILE in stdio..
    ... global array of FILE objects. ... The first fix was to move the locking fields ... into a private 'struct __sFILEX' to preserve the size of FILE. ... not grub around in the internals). ...
    (freebsd-arch)
  • Re: function template
    ... > I was working with a simple function template to find the min of two values. ... struct Promote ... template struct Promote{typedef unsigned long ... template struct Promote{typedef int ...
    (comp.lang.cpp)
  • Re: Stable priority queue
    ... > If a 5GHz computer had an integer increment that took only one CPU cycle ... We could also use the sizeof trick though, which is a cool use of template ... struct typelist ... typedef T car; ...
    (comp.lang.cpp)
  • curiously recurring template pattern problem
    ... typedef typename Derived::type type; ... struct Y: public X ... derived type as a template parameter to X (aka the curiously recurring ...
    (comp.lang.cpp)
  • Re: template typedef
    ... template <typename T> ... Is there a way to typedef the first part, so I can do something like: ... struct typedXptr { ...
    (microsoft.public.vc.language)