Reputation: 38919
Typically I'll preserve function arguments something like this:
template<typename T>
struct foo {
using type = T;
};
I know it's not going to be using
but in what way can I preserve a function passed as a template parameter?
template<void (*T)(const int)>
struct bar {
static T& type = T;
};
I know this doesn't work, I just want to know how I can go about the act of preserving this function pointer.
Upvotes: 0
Views: 51
Reputation: 180710
You can use decltype
to declare a member variable of the type of T
that people can refer to like
template<void (*T)(const int)>
struct bar {
static constexpr decltype(T) value = T;
};
You can take it a step farther and make the type of T
a member as well like
template<void (*T)(const int)>
struct bar {
using fptr = decltype(T)
static constexpr fptr value = T;
};
Upvotes: 3
Reputation: 62583
When used like this:
template<void (*T)(const int)>
struct bar {
static T& type = T;
};
T is a non-type template parameter, and there is no type to preserve (I would also suggest a different name for it, since T usually refers to type)
You can make a pointer itself available as a member of the struct, for example
static constexpr int (*func_ptr)(int) = T;
Upvotes: 2