c++ - Pointer to template function which is NOT member of any class -
i have pointer template function has 2 parameters of type t
.
template <typename t> typedef bool( * f )( t, t ); template <typename t> bool mniejsze (t pierwszy , t drugi){ if( pierwszy < drugi) return true; return false; } template <typename t> bool wieksze (t pierwszy, t drugi){ if( pierwszy > drugi ) return true; return false; }
but get:
error: template declaration of 'typedef'|
edit: pass pointer: right way?
template <typename t> t minmax(t a[], int n,bool &f){ return f(a[0],a[1]); }
in c++11 can use aliases:
template<typename t> using f = bool( *)( t, t );
usage:
f<int> f1 = wieksze; f1( 3, 4);
in c++03 there workoround:
template<typename t> struct f { typedef bool( *type)( t, t ); };
usage:
f<int>::type f1 = mniejsze<int>; f<int>::type f2 = mniejsze<int>; f1( 3, 4); template<typename t> t minmax(t a[], int n, typename f<t>::type fp ){ if ( fp( a[0], a[1])) return 1; return -1; } int main() { // code goes here f<int>::type f1 = wieksze<int>; bool b = f1( 3, 4); int a[] = { 3, 4}; std::cout << minmax<int>( a, 0, f1); return 0; }
Comments
Post a Comment