-1

I would like to have pointer to template function which 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 I get:

 error: template declaration of 'typedef'|

EDIT: Then I would like to pass that pointer: Is it the right way?

template <typename T>
T minmax(T a[], int n,bool &f){
    return f(a[0],a[1]);

}
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • C++11 support? Do you understand you will get a `template` type, and to get the pointer you will have to supply `T`? – Yakk - Adam Nevraumont Apr 07 '14 at 08:36
  • possible duplicate of [C++ template typedef](http://stackoverflow.com/questions/2795023/c-template-typedef) – MatthiasB Apr 07 '14 at 08:36
  • @MatthiasB It is not duplicate I ask about pointer not template function. – Yoda Apr 07 '14 at 08:38
  • Have you tried the approaches in the link and they didn't work? It should be the same concept for template functions and pointer to template functions – MatthiasB Apr 07 '14 at 08:39
  • @MatthiasB My template functions WORK, pointer does not. – Yoda Apr 07 '14 at 08:40
  • Duplicate of http://stackoverflow.com/questions/4573941/c-function-pointer-to-the-template-function-pointer you can't have a plain old pointer to *any* template function, just concrete type function pointer. – vz0 Apr 07 '14 at 08:42
  • Have you try `template using f = bool(*)(T, T);` ? – Jarod42 Apr 07 '14 at 08:42
  • As you can see in the answers, these are the exact solutions of the duplicate I posted. The problem is that you are trying to typedef a Template, not that you are trying to typedef a function pointer. You can see a online working example here: https://ideone.com/jaFUrB – MatthiasB Apr 07 '14 at 08:46
  • OUTSIDE THE CLASS OR STRUCTURE! – Yoda Apr 07 '14 at 08:53

3 Answers3

2

In C++11 you can use aliases:

template<typename T>
using f = bool( *)( T, T );

usage:

f<int> f1 = wieksze;
f1( 3, 4);

http://ideone.com/KyUjwP

In C++03 there is a 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() {
    // your 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;
}

http://ideone.com/Dh2eEN

4pie0
  • 29,204
  • 9
  • 82
  • 118
1

In C++11 you can do:

template<class T>
using f = bool(*)(T, T);

And use:

f<int> fp = wieksze;
Jamboree
  • 5,139
  • 2
  • 16
  • 36
1

How about the following?

 template <typename T>
  struct FNPTR {
    typedef bool (*f)( T, T );
 };

Put a dummy wrapper of a struct, and use FNPTR::f later? For example,

FNPTR::f = mniejsze<char>;

This should work!

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69