wrote in news:1110709181.652413.19730@o13g2000cwo.googlegro ups.com in
comp.lang.c++:
[color=blue]
>
> I understand that I could always settle for this syntax fairly easily:
>
> functor<bool, long, long> myFunctor;
> bool whether = myFunctor(4,5);[/color]
#include <iostream>
#include <ostream>
int function( int a, int b )
{
std::cout << "function( " << a << ", " << b << " );\n";
return 0;
}
/* declaration, no defenition
*/
template < typename F > struct functor;
/* Example, just the one specialization
*/
template < typename R, typename A1, typename A2 >
struct functor< R( A1, A2 ) >
{
functor( R arg(A1, A2) ) : f( arg ) {}
R operator () ( A1 a1, A2 a2 )
{
return f( a1, a2 );
}
private:
R (*f) ( A1, A2 );
};
int main()
{
functor< int( int, int ) > f( function );
return f( 1, 2 );
}
You will need to provide a specialization for each arity (number
of arguments) you want to support, also you might want to use
somthing to add `const &` to the paramiter types of the operator.
Untested code:
....
R operator () (
typename add_cref< A1 >::type a1,
typename add_cref< A2 >::type a2
)
{
return f( a1, a2 )
}
....
template < typename T > struct add_cref
{
typedef T const &type;
};
template < typename T > struct add_cref< T const & >
{
typedef T const &type;
};
template < typename T > struct add_cref< T & >
{
typedef T &type;
};
HTH.
Rob.
--
http://www.victim-prime.dsl.pipex.com/