专门针对具有不同签名的功能的模板?

时间:2022-05-15 16:52:11

I have a template that requires that I pass it a functor as a type parameter in order to perform some calculations. I would like to specialize this functor based on another function that I want to use to actually perform the calculations. Basically, I want to do this (which is not legal, I'm redefining the functor):

我有一个模板,要求我传递一个仿函数作为类型参数,以执行一些计算。我想根据我想用来实际执行计算的另一个函数来专门化这个仿函数。基本上,我想这样做(这不合法,我正在重新定义仿函数):

template<typename T, int (*func)(int)>
struct Functor
{
    T val;
    int operator()(int x) { return func(2); }
};

template<typename T, int (*func)(int, int)>
struct Functor 
{
    T val;
    int operator()(int y) { return func(y, 2); }
};

Component<Functor<calculationFunction1>> comp1;
Component<Functor<calculationFunction2>> comp2;

auto result1 = comp1.Compute();
auto result2 = comp2.Compute();

I've tried using partial specialization to get this to work as well, but that doesn't seem to be legal, either. I'm not sure if it's possible to get what I want, since the two functions have differing signatures. What's the best way to achieve what I'm trying to do here?

我尝试过使用部分专业化来实现这一点,但这似乎也不合法。我不确定是否有可能得到我想要的东西,因为这两个函数有不同的签名。实现我在这里尝试做的最好的方法是什么?

1 个解决方案

#1


-1  

How about:

template<typename T, typename SOME_FUNC_TYPE, SOME_FUNC_TYPE func >
struct Functor {};

typedef int (*SingleArgFunc)(int);
typedef int (*DoubleArgFunc)(int,int);

template<typename T, SingleArgFunc func>
struct Functor<T, SingleArgFunc, func>
{
  T val;
  int operator()(int x) { return func(2); }
};

template<typename T, DoubleArgFunc func>
struct Functor<T, DoubleArgFunc, func>
{
  T val;
  int operator()(int y) { return func(y, 2); }
};

int calculationFunction1 (int) { return 0; }
int calculationFunction2 (int, int) { return 0; }

template <typename FUNCTOR>
struct Component
{
  int Compute (void) { return FUNCTOR()(0); }
};

Component<Functor<int, decltype(&calculationFunction1), calculationFunction1>> comp1;
Component<Functor<int, decltype(&calculationFunction2), calculationFunction2>> comp2;

auto result1 = comp1.Compute();
auto result2 = comp2.Compute();

#1


-1  

How about:

template<typename T, typename SOME_FUNC_TYPE, SOME_FUNC_TYPE func >
struct Functor {};

typedef int (*SingleArgFunc)(int);
typedef int (*DoubleArgFunc)(int,int);

template<typename T, SingleArgFunc func>
struct Functor<T, SingleArgFunc, func>
{
  T val;
  int operator()(int x) { return func(2); }
};

template<typename T, DoubleArgFunc func>
struct Functor<T, DoubleArgFunc, func>
{
  T val;
  int operator()(int y) { return func(y, 2); }
};

int calculationFunction1 (int) { return 0; }
int calculationFunction2 (int, int) { return 0; }

template <typename FUNCTOR>
struct Component
{
  int Compute (void) { return FUNCTOR()(0); }
};

Component<Functor<int, decltype(&calculationFunction1), calculationFunction1>> comp1;
Component<Functor<int, decltype(&calculationFunction2), calculationFunction2>> comp2;

auto result1 = comp1.Compute();
auto result2 = comp2.Compute();