Erste boost :: function arity bei der Kompilierung?
-
02-10-2019 - |
Frage
Ich brauche eine Entscheidung in einer BOOST_PP_IF
Aussage über die arity (Parameteranzahl) ein boost::function
Objekt basieren. Ist das möglich?
boost::function_types::function_arity
tut, was ich suche, aber zur Laufzeit; Ich brauche es bei der Kompilierung.
Lösung
Aus irgendeinem Grund meine beinhaltet keep Brechen, aber nicht in der Vorschau = [
#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);
}
Andere Tipps
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
Notiz, das ist die Kompilierung konstant
Sie sollten hier beginnen: http://www.boost.org/doc/ libs / 1_43_0 / libs / mpl / doc / index.html
oder Verwendung BOOST_PP_SEQ_FOR_EACH / BOOST_PP_REPEAT_FROM_TO if / else Bedingungen gegen function_arity<F>::value
erzeugen
Wenn alles, was Sie brauchen, ist die arity einer boost :: Funktion zu lesen, dann brauchen Sie nicht so viel Arbeit zu tun:
#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;
}