Original Post
Sort of theoretical problem from the land of C++. I would like to create a templated struct/class that has a function as a template parameter. It would be a simple one-argument function. The type of the struct is meaningless, since it would be used in expressions, so template arguments can be deduced as much as possible. The goal is: as little verbosity as possible. First sample: Too verbose - I have to write types by hand. The name of the function should be enough (forget about the overloads for the moment). Second sample - let's use some deducing: But I have to use a macro, which is polutting the space all around.
I'm out of ideas for now. Is it possible to do this with templates only?
template< typename R, typename Arg, R Fun(Arg) >
struct S
{ /*...*/ };
/* usage: */
int my_fun(float&);
S<int, float&, my_fun>()
template< typename R, typename Arg >
struct fun_type_deducer
{
template< R Fun(Arg) >
S<R, Arg, Fun>
instantiate(void) const {
return S<R, Arg, Fun>();
};
};
template< typename R, typename Arg >
inline fun_type_deducer<R, Arg>
get_fun_type_deducer( R Fun(Arg) ) {
return fun_type_deducer<R,Arg>();
};
#define SS(Fun_) ( (get_fun_type_deducer(Fun_)).instantiate<(Fun_)>() )
/* usage: */
int my_fun(float&);
SS(my_fun)
I'm out of ideas for now. Is it possible to do this with templates only?