无法获取模板函数的函数指针 - templateception

时间:2013-12-05 20:01:05

标签: c++ function templates

我想将带有模板参数的模板函数传递给函数指针但是我没有把它运行起来:

template <class T>
class tupel
{
public:
    tupel<T>(T x, T y) : x(x), y(y) {};
    T x;
    T y;
};

typedef tupel<int>(*fp)(tupel<int>);

template <tupel<int> &min, tupel<int> &max>
tupel<int> resize(tupel<int> size)
{
    tupel<int> result_size;

    result_size.x = (size.x >= max.x)?max.x:(size.x<=min.x)?min.x:size.x;
    result_size.y = (size.y >= max.y)?max.y:(size.y<=min.y)?min.y:size.y;

    return result_size;
}

void print_tupel(tupel<int> t)
{
    std::cout << t.x << "," << t.y << "\n";
}

int main(int argc, char **argv)
{    
    tupel<int> & tp = *(new tupel<int>(600,80));

    tupel<int>(*foo) &max>(tupel<int>) = resize<tp, tp>;

    return 0;
}

如何获得此模板函数的函数指针?

2 个答案:

答案 0 :(得分:1)

这是您的计划的运作方式:

#include<iostream>
using namespace std;

template<class T>
class tupel
{
public:
    tupel<T>()
        : x(0), y(0)
    { }
    tupel<T>(T x, T y)
        : x(x), y(y)
    { }

    T x;
    T y;
};

typedef tupel<int> (*fp) (tupel<int>, tupel<int>, tupel<int>);

template<class T>
tupel<int> resize(T size, T max, T min)
{
    tupel<int> result_size;

    result_size.x = (size.x >= max.x) ? max.x : (size.x <= min.x) ? min.x : size.x;
    result_size.y = (size.y >= max.y) ? max.y : (size.y <= min.y) ? min.y : size.y;

    return result_size;
}

void print_tupel(tupel<int> t)
{
    std::cout << t.x << "," << t.y << "\n";
}

int main(int argc, char * * argv)
{
    tupel<int>& tp1 = *(new tupel<int>(600, 80));
    tupel<int>& tp2 = *(new tupel<int>(100, 800));
    tupel<int>& tp3 = *(new tupel<int>(200, 200));

    // assigning function pointer for template function.
    fp foo;
    foo = &resize<tupel<int>>;
    tupel<int> result = (*foo)(tp1, tp2, tp3);

    print_tupel(result);

    return 0;
}

答案 1 :(得分:0)

当我刚尝试调用resize时,编译器就没有任何东西了:

tupel<int> tp(600,80); 
tupel<int> tumin(100, 200), tumax( 200, 800) ;

tupel<int> result= resize<tumin, tumax>( tp) ;

t.cpp:35:21: error: no matching function for call to 'resize'
        tupel<int> result= resize<tumin, tumax>( tp) ;
                           ^~~~~~~~~~~~~~~~~~~~~~~~~
t.cpp:15:13: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'min'
tupel<int> resize(tupel<int> asample)

我建议改为创建一个带存储的仿函数:

template <class T>
  class resizor
  { 
    public:
      resizor(T amin, T amax) : min_(amin), max_(amax) { }
      T operator()(T & asample)
      {
        T result= asample ;

        result.x = (asample.x >= max_.x)?max_.x:(asample.x<=min_.x)?min_.x:asample.x;
        result.y = (asample.y >= max_.y)?max_.y:(asample.y<=min_.y)?min_.y:asample.y;
        return result ;
      }

    private:
      T min_, max_ ;
  } ; 

辅助函数使其更清晰:

template <class T>
  resizor<T> make_resizor(T amin, T amax) { return resizor<T>( amin, amax) ; } 

然后你可以这样做:

int main(int argc, char **argv)
{
  tupel<int> tp(600,80);
  auto myresize= make_resizor( tupel<int>(100, 200), tupel<int>(400, 800) ) ; 

  tupel<int> result= myresize( tp) ; 
  print_tupel( result) ; 

  return 0;
}

哪个输出:

  

400200