我需要根據boost::function
對象的參數(參數數量)在BOOST_PP_IF
語句中做出決定。這可能嗎?編譯時獲取boost :: function arity嗎?
boost::function_types::function_arity
做我在找什麼,但在運行時;我在編譯時需要它。
我需要根據boost::function
對象的參數(參數數量)在BOOST_PP_IF
語句中做出決定。這可能嗎?編譯時獲取boost :: function arity嗎?
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);
}
這完美地解決了我的問題。謝謝! – Xtapolapocetl 2010-09-15 16:04:04
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產生,如果對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;
}
這並不是在編譯時按照需求獲得函數的arity。 – Joel 2013-07-12 19:53:21
預處理沒有的類型或值的概念,除了整數文字。它不知道一個函數是什麼。 – GManNickG 2010-09-15 04:29:24
只有模板爲您提供編譯時分支,而不是在編譯之前作爲單獨階段運行的預處理器。 – 2010-09-15 04:39:17