[Solved] boost::any question

Started by
4 comments, last by ZealousEngine 16 years, 11 months ago
I want to do something like this...

void goo( int i ) {}

void foo( int id, boost::any arg1 ) {
	if ( id == 1 ) goo(  boost::any_cast<arg1.type>(arg1) );
}
...so, you call foo() with a id (which defines what function to call, there could be more than 1), and a argument of an unknown type. Then, when foo() figures out what to call, it uses an anycast to convert arg1 FROM boost::any, BACK to its original type. However, the above code doesnt work, im guessing I cant use arg1.type as an actual type? [Edited by - ZealousEngine on May 16, 2007 8:00:39 PM]
Advertisement
I guess the real question is, how can you 'store' a TYPE, and then use it again later to do a cast?
Quote:Original post by ZealousEngine
I want to do something like this...

void goo( int i ) {}void foo( int id, boost::any arg1 ) {	if ( id == 1 ) goo(  boost::any_cast<arg1.type>(arg1) );}



You can't. How would you like the compiler to treat your code?
First, how should it generate code for an unknown cast? And second, it's hard to generate code for a function call if you don't know which function is being called.

You need to know what type your data is to be able to work on it. You might use boost::any to temporarily stick it into an "unknown" type, but you have to know at compile-time which type you're trying to extract from it.
Well, I figured out a way to do it, its just a little bit of work..

template < class T > class Any {	public:		Any( T val ) { mVal = val; }		T get( void ) { return mVal; }	private:		T mVal;};//I know what the type is when I send it to foo(), so thats no problemAny<int> myAny(val);//now INSIDE foo(), it can call goo without having to know the type of the arggoo( myAny.get() );


The onlything I dont like about that method is, I need to convert all my args to my custom 'any' class before I pass them to foo, but I guess thats not too hard.

This method is also 100x faster than boost::any. I noticed converting a value into a boost any is INSANELY slow.

Can anyone think of a better way to do this? or should I stick with the above method?

*BTW, how does boost bind work when it comes to 'any type' arguments? I mean, I can say ' boost::bind( &foo, _1 )( 1 ) ', so how does _1 know to convert to a int?

*hrmm wait a minute.. why dont I just make foo() a template function.. then foo() can worry about what types im passing in... yes that should work...

[Edited by - ZealousEngine on May 16, 2007 6:12:53 PM]
Quote:Original post by ZealousEngine
//I know what the type is when I send it to foo(), so thats no problem
Any<int> myAny(val);

//now INSIDE foo(), it can call goo without having to know the type of the arg
goo( myAny.get() );


Well, it doesn't do the same thing as boost::any.
The point in boost::any is that you can convert anything to one common type (boost::any), and so store all sorts of differently typed objects in the same container, for example.

That's not possible with your class. myAny<int> is a different type from myAny<float>, so you can't put both into the same vector, for example.

So yeah, if you don't actually need boost::any's functionality, there's not much point in using it, and not using it will be quite a bit more efficient too.

Also, is there any particular reason why you use this class at all? Why not just pass the raw int around instead of myAny<int>? If all the class does is allow you to retrieve the int it stores, what's the point?
Yeah after giving it some thought im going to use template functions.

Thanks for the help everyone!

This topic is closed to new replies.

Advertisement