Jump to content
  • Advertisement
Sign in to follow this  
ZealousEngine

[Solved] boost::any question

This topic is 4054 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

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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 problem
Any<int> myAny(val);

//now INSIDE foo(), it can call goo without having to know the type of the arg
goo( 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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!