parameter passing - C++, pointer to a function as a new type -
in c++ 2003, typedef may used on complete types. hence, not allow create pointer function , generic t type:
template <typename t> typedef t(*f_function)(t, t, t);
is there way how evade issue in c++ 2003 or in c++0x using syntax
using (*f_function)(t, t, t); //
i use pointer function fff class member
template <typename t> class { public: f_function fff; a() {fff = null;} a( f_function pf){fff = &pf;} }; template <typename t> t f1(t x, t y, t z) { return x + y + z;} template <typename t> t f2(t x, t y, t z) { return x - y - z;}
and initialize value in constructor. subsequently:
int main() { <double> a(f1); double res = a.getx(1.1, 2.2, 3.3); }
is construction safe? there other way how solve problem? help.
you may use alias template (c++11) declaration:
template <typename t> using f_function = t(*)(t, t, t);
example:
http://coliru.stacked-crooked.com/a/5c7d77c2c58aa187
#include <iostream> #include <string> #include <array> template <typename t> using f_function = t(*)(t, t, t); template <typename t> class { public: f_function<t> fff; a() {fff = null;} a( f_function<t> pf){fff = pf;} }; template <typename t> t f1(t x, t y, t z) { return x + y + z;} template <typename t> t f2(t x, t y, t z) { return x - y - z;} int main() { <double> a(f1); double res = a.fff(1.1, 2.2, 3.3); std::cout << res; }
Comments
Post a Comment