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
williamcampbellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
0
evilrixSenior 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}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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") }
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

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.
0
Infinity08Commented:
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

0
Let_Me_BeCommented:
Will be coming in C++0x (check your compiler for support, it might already be there).

http://en.wikipedia.org/wiki/C%2B%2B0x#Initializer_lists
0
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)?

http://www.cplusplus.com/reference/stl/map/
0
itsmeandnobodyelseCommented:
>>>> 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;
public:
    // 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.
0
williamcampbellAuthor Commented:
Thanks guys lots to think about
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.