Consider following case: I have
int bar1();
double bar2();
I want:
foo<bar1>(); // calls bar1, then uses its result.
foo<bar2>(); // calls bar2, then uses its result.
Naive way to write template foo()
is to use additional parameter:
template <typename T, T (*f)()> void foo () {
// call f, do something with result
}
This works, but I need to do ugly syntax:
foo<decltype(bar1()), bar1>(); // calls bar1, then uses its result
I want to write something pretty, like above, just foo<bar1>
.
P.S. Please do not recommend to accept argument at runtime. I need compile time parametrization with function pointer only.
P.S. Sorry forget to mention: I am looking for C++14 solution. C++17 appreciated and I upvoted answer with C++17 solution, but project now builds with C++14 and I can not change it in nearest future.