Re: How to convert a typedef to a std::function template argument?
Am 09.05.2012 21:27, schrieb Peter:
Hi Folks,
given this typedef
typedef bool (*on_hit_test_type)(widget_interactive *, point const*,
bool *);
and this member definition
std::function<bool (widget_interactive *, point const*, bool *)>
on_hit_test;
Is there a way to convert one to the other? (The typedef to the
std::function template argument or the other way round)
Do you mean something like this:
std::function<std::remove_pointer<on_hit_test_type>::type> on_hit_test;
in one direction? The other direction requires a little more efforts:
You could invent a small trait like the following
template<class>
struct function_signature;
template<class R, class... Args>
struct function_signature<std::function<R(Args...)>>
{
typedef R type(Args...);
};
Now you are able to go backwards via:
typedef std::add_pointer<function_signature<func_type>::type>::type fu_ptr;
The following assertion should hold:
static_assert(std::is_same<fu_ptr, on_hit_test_type>::value, "");
The reason I'm asking is that I have a ton of them, and it's easy to
make errors in these somwhat duplicate constructs.
Ideally, I'd like to write one and derive the other somehow from it.
I don't understand how you mean this - how is derivation related to that?
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]