Array of Type

I would like to have an array like this:

Hypothetical PseudoCode

struct mystruct
      char* tag;
      T type;

Completely Wrong but want to set up a const array like this
  const SomeStruct[] = { ( "tag1", double), ("tag2", int), ("tag3", char*)};

 Any way to do this using templates?

LVL 12
Who is Participating?
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
A similar (although not to do with arrays but to do with template value parameters) that I assisted on today that you might find useful to read. {http:/Q_24546546.html}
evilrixSenior Software Engineer (Avast)Commented:
>> Any way to do this using templates?
Specifically what do you want to do with templates and why do you think that would give a better solution?

BTW: Template values can only be integral types (numeric integer values known at compile time but not real numbers like float or double) and whilst char * is a valid value parameter for a template you can't actually pass a "string" to instantiate it because the address of the string is not known until runtime.
williamcampbellAuthor Commented:
Interesting read

      is something like below possible?

      template< class T>
      struct item
            item (std::sting i) { tag=i; }
            std::string tag;

      somearray = { new item<int> ( "tag1"), new item<double> ( "tag2") }
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

williamcampbellAuthor Commented:
Heres the scenario:

  Coming over the wire will be random string tags and some data. Depending on what the tag name is, determine the type from a const array (or vector or whatever) and interpret the data.
Infinity08Connect With a Mentor Commented:
I think what you want is a union inside the struct. Something like below.

You will of course have to define all types at compile time, but you can have an array of structs, whose contents can vary depending on the tag string.

Note that instead of a string for the tag, it might be more appropriate to use an enum ...
struct MyStruct {
  std::string tag;
  union {
    int value_int;
    double value_double;
    // ...
  } value;
MyStruct someArray[2];
someArray[0].tag = "tag1";
someArray[0].value_int = 5;
someArray[1].tag = "tag1";
someArray[1].value_double = 10.0;

Open in new window

Let_Me_BeConnect With a Mentor Commented:
Will be coming in C++0x (check your compiler for support, it might already be there).
evilrixSenior Software Engineer (Avast)Commented:
>> check your compiler for support, it might already be there
Any compiler that is TR1 ready will (should?) support this... but remember it is NOT portable until it is ratified.

Can't you just use a std::map? The key being the string tag and the value being whatever you need it to resolve to (a struct of const data or a polymorphic handler class for example)?
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> Can't you just use a std::map?

I recently had a project which has a dictionary similar to

class Dictionary : public Object
    std::map<std::string, Object*> > mymap;
    // populating all map member functions

Then they had class types for all C types (POD) using

template <class  T>
class PodType : public Object
typedef PodType<int> IntType;

as well as for all possible container types what made these types storable as Object pointers.

That way a Dictionary or Array could take any type as value type, including dictionaries and arrays ... It supported full copy semantics.

The only disadvantage was that the objects retrieved from those containers must be casted to their real type before use as not all fucntionality could be made via virtual functions.
williamcampbellAuthor Commented:
Thanks guys lots to think about
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.