8

对于普通的香草功能,它工作得很好。下面的代码工作得很好。它只打印应该的内容:

int __cdecl(int, char)
2
int,char

#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;
}

所以,问题是,如果 foo 是类 bar 的成员函数,怎么能做到这一点?

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

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

等等,等等……没有喜悦。

4

3 回答 3

9

Boost Function Types可能是自然的解决方案:

#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 <boost/typeof/typeof.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;
}
于 2010-01-29T20:48:04.060 回答
1

Kornel Kisielewicz 做到了。这是与测试消息分开的解决方案。

#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;
    int arity = boost::function_types::function_arity<foo_type>::value;
    typedef boost::mpl::at_c<boost::function_types::parameter_types<foo_type>,1>::type
        arg1;
    typedef boost::mpl::at_c<boost::function_types::parameter_types<foo_type>,2>::type
        arg2;

    std::cout << typeid(foo_type).name() << std::endl;
    std::cout << arity << std::endl;
    std::cout << typeid(arg1).name() << ",";
    std::cout << typeid(arg2).name() << std::endl;
    return 0;
}
于 2010-01-29T22:51:12.510 回答
1

这可以通过更新的 boost lib(可调用特征,奖励:它也适用于 lambdas)来完成。

我还添加了另一个带有人类可读输出的打印输出,尽管不确定问题是否需要它。

#include <boost/mp11.hpp>
#include <boost/callable_traits.hpp>
#include <iostream>
#include <type_traits>
#include <boost/type_index.hpp>
using namespace std;
using namespace boost::mp11;
using namespace boost::typeindex;
namespace ct = boost::callable_traits;

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

int main() {

    using Fn = decltype(&bar::foo);
    cout << typeid(Fn).name() << endl;
    cout << tuple_size<ct::args_t<Fn>>::value << endl; // includes implicit this
    cout << typeid(mp_at_c<ct::args_t<Fn>,0>).name() << ",";
    cout << typeid(mp_at_c<ct::args_t<Fn>,1>).name()  << endl;
    cout << "Human readable" << endl;
    cout << type_id<Fn>().pretty_name() << endl;
    cout << tuple_size<ct::args_t<Fn>>::value << endl; // includes implicit this
    cout << type_id<mp_at_c<ct::args_t<Fn>,0>>().pretty_name() << ",";
    cout << type_id<mp_at_c<ct::args_t<Fn>,1>>().pretty_name() << endl;
    return 0;
}

gcc 输出:

M3barFiicE

3

3bar,我

人类可读

int (bar::*)(int, char)

3

酒吧,int

于 2020-05-01T10:14:18.810 回答