Subtraction from the return type is not possible. Therefore, function2 cannot be deduced from what type of return value you expect.
However, the casting operator can be derived. Thus, you can replace function2 with a supporting structure, for example: Unfortunately, there is no standard syntax for declaring a statement operator when specifying an operator without a typedef, and type inference will not work through typedef. After completing the definitions in some compilers (works in g ++ 4.5, does not work in VC ++ 9):
struct function2 { template <typename T> (*operator bool())(T) { return function1<T>; } };
(see also C ++ conversion operator for conversion to a function pointer ).
The call should still look the same.
Note. C ++ 11 introduces an alternative typedef syntax that can be customized by templates. It would be like this:
struct function2 { template <typename T> using ftype = bool(*)(T); template <typename T> operator ftype<T>() { return function1<T>; } };
but I have neither g ++ 4.7 nor VC ++ 10, so I canβt check if it really works.
Added:
The trick in Boost.Lambda is that it does not return functions, but functors. And functors can be class templates. So you will have:
template<typename T> bool function1(T some_var) { return true; } class function2 { template <typename T> bool operator()(T t) { function1<T>; } }; template <typename F> void function3( F input_function ) { ... input_function(something) ... }
Now you can write:
function3(function2);
and he will solve the pattern inside function3 . All STLs accept functors as templates, so they will work with all STLs.
However, if you do not want to have function3 as a template, there is still a way. Unlike the function pointer, std::function (only for C ++ 11, use boost::function for older compilers), the template can be built from any functor (which includes simple function pointers). Therefore, given the above, you can write:
void function3(std::function<bool ()(char)> input_function) { ... input_function(something) ... }
and now you can still call:
function3(function2());
The fact is that std::function has a template constructor that internally generates a template wrapper and stores a pointer to its method, which cannot be called without additional templates.