C++: Giving unknown class variable to template -
i've got class abc , want give unknown class variable template, that:
template < v > class abc { // };
of course above code doesn't work (there wasn't type of parameter v). have got ideas fix that? don't want give type of variable v template.
there 2 functions, types different. how use both functions in class template below?
#include <iostream> using namespace std; // types of functions max1 , min1 different! int max1(int a, int b){ return a>b?a:b; } int& min1(int a, int b){ return a<b?a:b; } template<typename _t, _t(function)(_t,_t)> class abc { public: _t a, b; _t get() { return function(a, b); } }; abc <int, max1> abc; // <- if write "abc <int, min1> abc;", error comes! how fix this? int main() { abc.a = 3; abc.b = 8; cout << abc.get() << '\n'; cin.get(); return 0; }
seems irrelevant, question unclear @ beginning...
template < v >
must be
template < typename v >
or
template < class v >
typename
preferable.
regarding edit: problem is, min1
returns &
. if remove it, may have:
#include <iostream> int max1(int a, int b){ return a>b?a:b; } int min1(int a, int b){ return a<b?a:b; } template<typename _t, _t(*function)(_t,_t)> class abc { public: _t a, b; _t get() { return function(a, b); } }; int main() { abc <int, max1> abc; abc <int, min1 > def; abc.a = def.a = 3; abc.b = def.b = 8; std::cout << abc.get() << '\n'; std::cout << def.get() << '\n'; return 0; }
and print 8 3
Comments
Post a Comment