0

BOOST_PP_IF我需要根据对象的数量(参数计数)在语句中做出决定boost::function。这可能吗?

boost::function_types::function_arity做我正在寻找的,但在运行时;我在编译时需要它。

4

3 回答 3

2
function_arity

template<typename F>
struct function_arity;

Header

#include <boost/function_types/function_arity.hpp>

F

    Callable builtin type 
function_arity<F>

    Function arity as MPL - Integral Constant 
function_arity<F>::value

    Constant value of the function arity 

注意,这是编译时间常数

你应该从这里开始:http: //www.boost.org/doc/libs/1_43_0/libs/mpl/doc/index.html

或使用 BOOST_PP_SEQ_FOR_EACH/BOOST_PP_REPEAT_FROM_TO 生成 if/else 条件function_arity<F>::value

于 2010-09-15T04:56:17.410 回答
1

出于某种原因,我的包含不断中断但不在预览中=[

#include <ostream>  
#include <iostream>  
#include <boost/function.hpp>  

// Assume that you want to print out "Function is N-arity" for general case. But "nularity" for 0

template< int i >
struct DarkSide
{
template<class U>
void operator()(std::ostream& out, const U& u) { out << "Function is "<<i<<"-arity"<<u; }
void operator()(std::ostream& out, std::ostream& ( *pf )(std::ostream&) ) { out << "Function is "<<i<<"-arity"<<pf; }
};

template<>
struct DarkSide<0>
{
template<class U>
void operator()(std::ostream& out, const U& u) { out << "Function is nularity"<<u; }
void operator()(std::ostream& out, std::ostream& ( *pf )(std::ostream&) ) { out << "Function is nularity"<<pf; }
};

int main() {
typedef boost::function< void ( ) > vFv;
typedef boost::function< void ( int x ) > vFi;
DarkSide< vFv::arity >()(std::cout,"\n");
DarkSide< vFi::arity >()(std::cout,std::endl);
}
于 2010-09-15T12:22:08.597 回答
-1

如果您只需要阅读 boost::function 的数量,那么您不需要做那么多工作:

#include <boost/function.hpp>
#include <iostream>
int main() {
  std::cout << boost::function<void()>::arity << std::endl;
  std::cout << boost::function<void(int)>::arity << std::endl;
  std::cout << boost::function<void(int, int)>::arity << std::endl;
}
于 2013-07-12T19:29:40.150 回答