如何将指向模板函数的指针传递给另一个函数

时间:2014-04-07 09:06:44

标签: c++ templates pointers

我得到了函数指针:

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

template <typename T>
bool mniejsze (T pierwszy , T drugi){
    if( pierwszy < drugi)
        return true;
    return false;
}

然后我定义了函数minamax

 template <typename T>

T minmax(T a[], int n,bool, bool (*f.type)(T,T)){
    return f(a[0],a[1]);

}

然后我想将它传递给函数minmax

  f<int>::type  f1 = mniejsze<int>;
    cout<<f1( 3, 4)<<endl;;
    int t[] = {1,2,3,4,5,6,7,8,9,10};
    int n  = 10;
    minmax(t,n,*f1);

但我明白了:

C:\Documents and Settings\Duke\Moje dokumenty\Andrzej1\adsadasd\main.cpp|57|error: expected ',' or '...' before '.' token|
C:\Documents and Settings\Duke\Moje dokumenty\Andrzej1\adsadasd\main.cpp||In function 'int main()':|
C:\Documents and Settings\Duke\Moje dokumenty\Andrzej1\adsadasd\main.cpp|72|error: no matching function for call to 'minmax(int [10], int&, bool (&)(int, int))'|
C:\Documents and Settings\Duke\Moje dokumenty\Andrzej1\adsadasd\main.cpp|68|warning: unused variable 'f2'|
||=== Build finished: 2 errors, 1 warnings ===|

1 个答案:

答案 0 :(得分:0)

template<typename T>
T minmax(T a[], int n, bool b, 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 = mniejsze<int>;
    bool b = f1( 3, 4);
    int a[] = { 3, 4};
    std::cout << minmax<int>( a, 0, 0, f1);
    return 0;
}

http://ideone.com/Dh2eEN