質問

で決定する必要があります BOOST_PP_IF のアリティ(パラメーターカウント)に基づくステートメント 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を使用してif/else条件を生成する function_arity<F>::value

必要なのは、ブースト::関数の不安を読むことだけなら、それほど仕事をする必要はありません。

#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