1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <boost\preprocessor.hpp>
#define MAX_FONCTION_ARITY 10
#define MA_FONCTION(z, N, text)\
class FonctionImpl##N \
{\
public:\
typedef double (*FPtr)(BOOST_PP_ENUM_PARAMS(N, double x));\
FonctionImpl##N(FPtr fonction) : m_fonction(fonction) {}\
double operator()(BOOST_PP_ENUM_PARAMS(N, double x)){ return m_fonction(BOOST_PP_ENUM_PARAMS(N, x));}\
private:\
FPtr m_fonction;\
};
BOOST_PP_REPEAT(MAX_FONCTION_ARITY,MA_FONCTION,"")
template<unsigned int N> struct FonctionImplHelper;
#define MA_FONCTION_IMPL_HELPER(z,N,text)\
template<> struct FonctionImplHelper<N> { typedef FonctionImpl##N type; };
BOOST_PP_REPEAT(MAX_FONCTION_ARITY,MA_FONCTION_IMPL_HELPER,"")
#undef MA_FONCTION_IMPL_HELPER
#undef MA_FONCTION
#undef MAX_FONCTION_ARITY
template<unsigned int N>
class Fonction : public FonctionImplHelper<N>::type
{
typedef typename FonctionImplHelper<N>::type base;
typedef typename base::FPtr FPtr;
public:
Fonction(FPtr f):base(f){}
};
#include <iostream>
double f0()
{
std::cout<<"f0\n";
return 0.;
}
double f1(double)
{
std::cout<<"f1\n";
return 1.;
}
double f5(double,double,double,double,double)
{
std::cout<<"f5\n";
return 5.;
}
int main()
{
Fonction<0> op0(f0);
op0();
Fonction<1> op1(f1);
op1(1.);
Fonction<5> op5(f5);
op5(1.,2.,3.,4.,5.);
return 0;
} |
Partager