Skip to main content
GameDev.net gamedev.net
🔒 Locked

Boost Function

Started by Ryan_001 Apr 11, 2009 at 10:34 PM 3 replies 1.9k views
Original Post
Ryan_001
Ryan_001
Boost function has 2 ways to define a function, the portable syntax and the preferred syntax. The portable seems pretty simple, a function like:
int func (bool);
is defined like:
boost::function<int,bool> f;
The preferred syntax for the same function looks like:
boost::function<int (bool)> f;
Now the question is, how would you write a template to support the preferred syntax? What I mean is, if I were to write a template class to support the portable syntax, I imagine it'd look something like this:

template <typename TR = void, typename T1 = void, typename T2 = void /* ect... */ > class FunctionWrapperOfSomeSort {};

template <> class FunctionWrapperOfSomeSort<void,void,void> {};
template <typename TR> class FunctionWrapperOfSomeSort<TR,void,void> {};
template <typename TR, typename T1> class FunctionWrapperOfSomeSort<TR,T1,void> {};

// ect...

But I'm not sure how I'd write a template class to accept something along the lines of Function, like the boost function preferred syntax. I tried looking at the boost function source, but its filled with macros, and I can't figure out what they're doing. Anyone have any thoughts?
Telastyn
Telastyn
Quote:
Original post by Ryan_001
Anyone have any thoughts?


That it is by and large useless to know the black magic that makes it work.

[edit: let me rephrase... you could use your time better learning something more practical]
Deyja
Deyja
Short answer is, you can't.

Long answer is, you need to write a whole lot of templates. The reason the boost headers are a macroed mess is because they are using boost::preprocessor to generate a whole lot of template overloads and specializations.

Ultimately, however, it appears to all boil down to a template declared how you would handle the 'portable' syntax. The stuff to handle the preferred syntax looks like it's just a wrapper.
Darklighter
Darklighter
int (bool) denotes the type of a function that takes a boolean parameter and returns an integer. Here's an alternative way of writing the declaration of such a function:

typedef int Type(bool);
Type function;


You can also use this notation for declaring function pointers. The following are equivalent:

typedef Type* ptr;
typedef int (*ptr)(bool);


Here's an example showing how to write a simple wrapper for function pointers, using the above techniques:

#include <iostream>#include <boost/type_traits.hpp>template<typename F> class Functor{	F* functor;	public:		explicit Functor(F* f): functor(f) {}		typename boost::function_traits<F>::result_type operator()(typename boost::function_traits<F>::arg1_type argument) const		{			return (*functor)(argument);		}};bool IsPositive(int value){	return value > 0;}int main(){	Functor<bool (int)> functor(IsPositive);	std::cout << functor(5);}


Note that boost::function_traits derives the types using template specialization.
Ryan_001
Ryan_001
I see, using function_traits to determine the function types. That I didn't think of, thx ; )

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.