There must be a difference between a typedef of a function and the use of a bare function type when used as a template type parameter.
I.e., consider
#include <functional>
typedef std::function<void(int)> TF1;
typedef void(*FooFn)(int);
typedef std::function<FooFn> TF2;
int main() {
TF1 tf1;
TF2 tf2;
return 0;
}
I can create a TF1
but not a TF2
(error: aggregate 'TF2 tf2' has incomplete type and cannot be defined
). (See ideone example.)
Is there a way to use a typedef of a function (signature) as a template type parameter; specifically, as the type parameter to std::function
?
(No C++11 tag because I'm interested in boost::function
as well on non-modern compilers. But a C++11 answer would also be appreciated, if the language changed in some way to enable this.)
std::function
needs a function type, while FooFn
is a pointer (to function) type, not a function type. Use the metaprogramming helper template remove_pointer
to convert it:
typedef std::function<std::remove_pointer<FooFn>::type> TF2;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With