My question is about having the compiler infer the return type of a function based on the return type of a function passed by template.
Is there some way I can call as
foo<bar>(7.3)
instead of
foo<double, int, bar>(7.3)
in this example:
#include <cstdio>
template <class T, class V, V (*func)(T)>
V foo(T t) { return func(t); }
int bar(double j) { return (int)(j + 1); }
int main() {
printf("%d\n", foo<double, int, bar>(7.3));
}
If you want to keep
bar
as a template argument, I'm afraid you can only get close to that:You could also define a macro if you want to avoid repeating
bar
's name:Alternatively, you could let
bar
become a function argument, which could make your life easier: