سؤال

أحتاج إلى اتخاذ قرار في 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 of A 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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top