If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

STL Style function parameters?

Hi,

I really like how the STL allows you to toss in a struct with a certain function present and it will use it like a function pointer (i.e. the various operators for sorting routines etc).

I'd like to use something similar myself, but the code is rather convoluted and I wasn't able to figure out how they are doing it so that I don't actually have to pass the type as a template parameter.

Does anyone know the trick they use that allows it? Is it really just looking for a function pointer or is there some sort of magic template trick that is letting that happen. If I try to use templates, I have to declare the correct type ahead of time, which doesn't work for what I want to do with it (I want that function object to be used only for that one function, and it needs to maintain some state).

Re: STL Style function parameters?

Originally Posted by e_torstenson

Does anyone know the trick they use that allows it? Is it really just looking for a function pointer or is there some sort of magic template trick that is letting that happen. If I try to use templates, I have to declare the correct type ahead of time, which doesn't work for what I want to do with it (I want that function object to be used only for that one function, and it needs to maintain some state).

The function template simply uses the object passed as if it were a function. If a function pointer is passed, then well and good. If a function object is passed, then operator() is overloaded, and it is also well and good. The template argument can be deduced from the type of the argument, whether function pointer or function object.

For this trivial example, it works fine, and allows me to skip having to use template parameters at the calling end, which is a goal, since it makes it a bit more readable. Thanks to laserlight for clarifying the objective with STL...I've spent the time thinking of the typing at the class level, and not at the function level.

PS I'm a little embarassed that I didn't see this post in my search though Hopefully, it's OK to have two similar threads.

Re: STL Style function parameters?

Actually, I have a related question. If I want to create a "task queue", where each task is a functor, there is a difficulty: Each functor must be of the same type in order to fit in an STL container, and the type must be recovered entirely before it can be run if you used something generic.

Is there a typical solution to this dilemma? I know boost::variant addresses it to some degree, but that still only gives you a limited number of types to work with.

Re: STL Style function parameters?

That's the obvious one, yes. I was just hoping to avoid that if I could. I'd like an interface comparable to boost::thread, which has no such limitation of course. Plus, functors should typically be copyable, and there's problems making that assumption when using inheritance.....

Re: STL Style function parameters?

Re: STL Style function parameters?

It depends on what you really mean by "comparable to boost::thread" - or more specifically, what "comparable" means.

A boost::function object becomes a type.

You can't create a container, for example, like:

list<boost::function *>;

You must supply template parameters, which then limits you to whatever target object and function signature you've supplied.

If you want want is a very generic container of functions to be called, such that entries in the container could perform a call to any object, and any member function signature, you'll have to wrap a generic object around the boost::function which itself is a non-template base with a virtual "callback" function.

The good news, though, is that you can fashion the object representing the queue such that it's something like the boost::thread. My own operates this way:

Re: STL Style function parameters?

There's only going to be one possible function signature----returning void with no arguments. So long as boost::function narrows all possible types with this signature down to a single type, it'll do. If that's not what it does, I may still have trouble....

Or, the class owning the queue would likely have a function "like" make_adapter which performed the submission, like:

Que<SomeObject> q;

q.Call( myo, &SomeObject::func );

Or if there's always only the same myo, perhaps its....

Que<SomeObject> q( myo );

q.Call( &SomeObject::fund );

Where myo is always the same instance for q.

Notice the use of the make_adapter function.

The issue is that any such template object is going to need parameters (objects can't deduce the parameters), but template functions can...hence the make_adapter to simplify application code interface.

Last edited by JVene; July 28th, 2009 at 03:43 PM.

If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.