使用Boost获取arity和paramerter类型的成员函数?(升压:: function_traits)

Jiv*_*son 7 c++ boost function member

它适用于普通的香草功能.下面的代码工作得很好.它打印的应该是什么:

int __cdecl(int, char)
2
int,char
Run Code Online (Sandbox Code Playgroud)
#include <boost/type_traits.hpp>
#include <boost/function.hpp>
#include <boost/typeof/std/utility.hpp>

#include <iostream>

using std::cout;
using std::endl;

int foo(int, char) {
 return 0;
}
int main() {
    typedef BOOST_TYPEOF(foo) foo_type;;
    typedef boost::function_traits<foo_type> function_traits;

    cout << typeid(foo_type).name() << endl;
    cout << function_traits::arity << endl;
    cout << typeid(function_traits::arg1_type).name() << ",";
    cout << typeid(function_traits::arg2_type).name() << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以,问题是,如果foo是类栏的成员函数,怎么能这样做呢?

struct bar {
    int foo(int, char) { return 0; }
};
Run Code Online (Sandbox Code Playgroud)

我尝试过这些结构的无数组合:BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()BOOST_TYPEOF_REGISTER_TYPE()boost :: ref boost :: remove_pointer boost :: bind boost :: mem_fn

等等...没有快乐.

Kor*_*icz 8

提升函数类型可能是自然的解决方案:

#include <boost/function_types/function_type.hpp>
#include <boost/function_types/parameter_types.hpp>
#include <boost/function_types/function_arity.hpp>
#include <boost/typeof/std/utility.hpp>
#include <iostream>

struct bar {
    int foo(int, char) { return 0; }
};

int main() {

    typedef BOOST_TYPEOF(&bar::foo) foo_type;

    std::cout << typeid(foo_type).name() << std::endl;
    std::cout << boost::function_types::function_arity<foo_type>::value << std::endl;
    std::cout << typeid(boost::mpl::at_c<boost::function_types::parameter_types<foo_type>,1>::type).name() << ",";
    std::cout << typeid(boost::mpl::at_c<boost::function_types::parameter_types<foo_type>,2>::type).name() << ",";

    return 0;
}
Run Code Online (Sandbox Code Playgroud)