I’ve been writing some templates that expect to be passed a boost::function. They determine the arguments to the boost::function and then use that to create an appropriate specialization of a class. For example, this is the template for 0 arguments:
template< class ReturnT >
MyFunctionPtr MakeFunction( boost::function< ReturnT ( ) > func )
{
return MyFunctionPtr( new MyFunction< decltype( func ), ReturnT >( func ) );
}
As a bit of a shortcut I’ve got another version of the MakeFunction template that takes in a function pointer and automatically wraps it in a boost::function of the appropriate type:
template< class T >
MyFunctionPtr MakeFunctionFromPointer( T func )
{
return MakeFunction(
boost::function< typename boost::remove_pointer<T>::type >( func )
);
}
This allows me to create from function pointers without explicitly passing in the function spec:
int something() { return 1; }
MakeFunctionFromPointer( &something );
On MSVC this is working fine but with GCC 4.3 I get “no matching function” errors. It appears that:
On MSVC boost::remove_pointer< bool (*)() >::type is bool ()
But on GCC 4.3: boost::remove_pointer< bool (*)() >::type is bool ()()
So on GCC an appropriate template of MakeFunction does not exist.
Is there any way I can alter the output on remove_pointer output on either platform to match the other? Or is there an other way I can go about solving this problem?
You can explicitly take a pointer as the function argument, and infer the function type from that:
UPDATE: based on your comment below, it looks like the problem is that you declare
MakeFunctionafter using it in the definition ofMakeFunctionFromPointer. Either my version, or yours usingboost::remove_pointer, should be fine as long as all functions are declared before use.