2010-07-09 39 views
0
#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; 

// (...) 

}; 
*/ 
+4

哈哈究竟是什么? StackOverflow不是编程Jeopardy。你不必以代码的形式来描述你的问题。 – 2010-07-09 17:59:48

+0

Lol,srsly把这个问题从代码xD – 2010-07-09 18:01:11

回答

1

你想提升的类型特征,具体功能特点:

http://www.boost.org/doc/libs/1_43_0/libs/type_traits/doc/html/boost_typetraits/reference/function_traits.html

它的工作原理只是普通的C函数指针细,无需使用boost ::为sigc ::信号的功能。通过函数特征,你可以单独提取返回类型和每个参数类型,不需要自己完成特殊化。

如果你根本不能使用boost,那么你仍然可以检查它们的实现以获得它的工作原理的一般概念。

+0

模板 struct function_traits: public detail :: function_traits_helper :: type> {}; woohoo,不能简单,因为除了这些宏是不可读的。这个想法在C++ 0x背后有一些神奇的东西,我不知道推动我问这个问题。是的,我需要把它写成“我自己”,它是一个所谓的学生项目。谢啦!不幸的是,我无法让你高兴。 – Willy 2010-07-09 18:31:40

相关问题