在编译时获得Boost ::功能障碍?
-
02-10-2019 - |
题
我需要在 BOOST_PP_IF
基于A的Arity(参数计数) boost::function
目的。这可能吗?
boost::function_types::function_arity
做我想要的,但是在运行时;我需要在编译时。
解决方案
由于某种原因,我的包括继续破裂,但不在preview = [[
#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来生成如果/其他条件 function_arity<F>::value
如果您只需要阅读boost ::函数的ARITY,那么您就不需要做这么多工作:
#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;
}
不隶属于 StackOverflow