Это работает просто великолепно для простых ванильных функций. Код ниже работает просто великолепно. Это печатает, что, должен:
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;
}
Так, вопрос, как можно сделать это, если нечто является функцией членства панели класса?
struct bar {
int foo(int, char) { return 0; }
};
Я попробовал бесчисленные комбинации этих конструкций: BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP () BOOST_TYPEOF_REGISTER_TYPE () повышение:: касательно повышения:: повышение remove_pointer:: свяжите повышение:: mem_fn
и т.д., и т.д. Никакая радость.
Типы функций усиления, вероятно, были бы естественным решением:
#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;
}
Корнел Кисиелвич прибил его. Здесь он с решением, отделенным от тестовых сообщений.
#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;
}