#include <iostream>
template<typename T_function_type>
struct pointer_wrapper {
T_function_type function_pointer;
explicit pointer_wrapper(T_function_type ptr) : function_pointer(ptr) { }
~pointer_wrapper() { }
};
template<typename T_return, typename T_arg1, typename T_arg2>
pointer_wrapper<T_return (*)(T_arg1, T_arg2)> fabricate_some_trait(T_return (*ptr)(T_arg1, T_arg2)) {
return pointer_wrapper<T_return (*)(T_arg1, T_arg2)>(ptr);
}
void hello(std::string const& name, std::string const& surname) {
std::cout << "Hi " << name << " " << surname << "! ";
std::cout << "Was wondering if it\'s possible to modify ";
std::cout << "some_trait template in that way that it\'s ";
std::cout << "capable of deducing function (as well as ";
std::cout << "static and member one) type like the ";
std::cout << "template function fabricate_some_trait is. ";
std::cout << "So I can use these traits in typedef. ";
std::cout << "Will be perfect if no <functional>, ";
std::cout << "Boost.Function nor sigc::signal is going ";
std::cout << "to be used. Hope you can help and sorry ";
std::cout << "in advance about the form of this question, ";
std::cout << "just feeling good today." << std::endl;
std::cout << "Cheers!" << std::endl;
}
int main() {
// need
// some_trait<hello>::function_type hello_pointer = hello;
// hello_pointer("Stackoverflow", "User");
fabricate_some_trait(hello).function_pointer("Stackoverflow", "User");
}
/*
template<typename T_return_type(*)(typename T_arg, typename T_arg2)>
struct some_trait {
typedef T_return (*function_type)(T_arg1, T_arg2);
typedef T_return result_type;
typedef T_arg1 arg1_type;
// (...)
};
*/
views:
62answers:
1
A:
You want boost's type traits, specifically the function traits:
It works just fine with normal C function pointers, no need to use boost::function for sigc::signal's. With the function traits you can separately extract the return type and each of the parameter types, no need to do the specialization yourself.
If you can't use boost at all, you could still examine their implementation to get the general idea of how it works.
Joseph Garvin
2010-07-09 18:07:54
template<typename Function>struct function_traits : public detail::function_traits_helper<typename boost::add_pointer<Function>::type>{};woohoo, cannot be simplier as that apart from the fact that those macros are unreadable. The thought there is some magic somewhere behind c++0x that I don't know about pushed me to ask this question. Yup, I need to wrote it "myself" 'couse its a so-called student project. Thanks man! Unfortunately I'm not able to upvote you.
Willy
2010-07-09 18:31:40