True 'AnyType' in C++

Started by
28 comments, last by Extrarius 20 years, 3 months ago
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++?
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
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.
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]
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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]
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
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.
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]
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
sig
http://gotw.ca/gotw/085.htm
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)
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk

This topic is closed to new replies.

Advertisement