Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Extrarius

True 'AnyType' in C++

This topic is 5309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For a project I''m working on, I need a class that can hold any type of value. For this, there is boost::any, BUT, in order to get the value you need to know what type it is and use the templated any_cast to get the value. I need to be able to get the value converted to a specific type without knowing what type the value was perviously. Is there any way to do this in C++?

Share this post


Link to post
Share on other sites
Advertisement
Long story short: not really.

However, one thing that could be done is store everything as strings. Then provided that all your types properly implement operator<< and operator>> you can convert things through stringstreams.

Share this post


Link to post
Share on other sites
I guess that is one way, but its not really viable. Constantly converting the types to/from a string would be very processor intensive =-/

[edited by - extrarius on January 5, 2004 2:05:23 PM]

Share this post


Link to post
Share on other sites
quote:
Constantly converting the types to/from a string would be very processor intensive =-/

Sounds like an assumption but I don''t know what you''re trying to do.

Share this post


Link to post
Share on other sites
I'm writing an interpreter, and using strings to store things would would mean running the value through the interpreter over and over.

[edited by - extrarius on January 5, 2004 2:14:42 PM]

Share this post


Link to post
Share on other sites
Well, other methods exist, but they tend to be type intensive. For example, type flag techniques. Without a better idea of what you''re trying it''s hard to give good advice here.

Share this post


Link to post
Share on other sites
if you have control over what this 'type' is then you could have a base class which everything is derived from. I can't see that being very useful on the whole. Usually runtime type information is useful for debugging and for editors. Are you wanting to make a scripting language or something? If you are I wouldn't worry too much about performance to start with.

[edited by - petewood on January 5, 2004 2:56:59 PM]

Share this post


Link to post
Share on other sites
maybe imjust talking out my ass, but u could have like context tables and interlink between and out of them. use the pointers to those tables, or have like a structure which has both type and pointer too.

variable
[
type enumVar;
void* myVarPtr;
]

call to new or malloc for 64 128 or 32 bit variants.
i would need more information on how you plan on storing and arranging variable execution with in your interpreted language before making a final call though

Share this post


Link to post
Share on other sites
I think the last few posters misunderstood my question. I don''t need a union that can hold one of 3-4 different types, I''m trying to make something simmilar to boost::any that can hold ANY type and doesn''t need to know anything about the types it can hold.
The problem with boost::any (well, not really a problem but something I''d like to change) is that in order to extract a value from it after you have stored the value, you have to know what type it is (which you can find out using the type method, which returns type_info of the type currently being stored and several if statements, but you have to know which types you''re going to handle to do that). I want to be able to say ''return the stored value as a float''(like float f = anyType.GetValue<Float>()) and if possible it will convert it to a float (if not, it should throw, or maybe it returns a pointer and returns null if it can''t convert). Right now, the boost::any class only allows you to retrieve the value in the type stored (if it is storing an int and you ask for a float, it will return a float pointer that points to the location of the int rather than converting the number stored)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!