|
Prev: why are foos like vector<T>::size no duplicate symbols for linker?
Next: Access internal methods in compounds?
From: Daniel on 28 Jun 2008 07:29 How do I write a template based on a function ? example: template <function F> class resmap { private: // some private declarations public: void callmyfunc(); }; template <function F>void resmap<F>:: callmyfunc() { // do some stuff temp = F(); // do more stuff } F is like a virtual function. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: huili80 on 29 Jun 2008 05:27
Not sure what you mean by "F is like a virtual function", but if F is something defined like the following: class F { public: some_return_type operator()() { // do some thing and return something of some_return_type } }; Then your resmap can be like this template <typename F> class resmap { private: // some private declarations public: void callmyfunc(); }; template <typename F> void resmap<F>::callmyfunc() { // do some stuff some_return_type temp = F()(); // do more stuff } In other words, F is some type that has operator(), and F() is a function object. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |