Вопрос

Мне нужно принять решение в BOOST_PP_IF Заявление, основанное на ARITY (количество параметров) boost::function объект. Это возможно?

boost::function_types::function_arity делает то, что я ищу, но во время выполнения; Мне это нужно во время компиляции.

Это было полезно?

Решение

По какой -то причине мой включает в себя продолжение, но не в предварительном просмотре = [

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

Другие советы

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, чтобы генерировать условия, если/else против против function_arity<F>::value

Если все, что вам нужно, это прочитать Arity от функции Boost :: вам не нужно делать так много работы:

#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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top