C++模板传递方法名,推导所有重载

问题描述 投票:0回答:0

我有简单的课

template <typename T>
using ConversionFunction = T(*)(T val);

static int Foo1(int x)
{
    return x * x;
}

static double Foo1(double x)
{
    return x * x + 1;
}


struct Foo
{
    ConversionFunction<double> d;
    ConversionFunction<int> i;
    ConversionFunction<float> f;
    
    template <typename T>
    void assign(){
        //??
    }
};

是否可以这样写:

Foo f;
f.assign<Foo1>();

assign
方法会自动获取指向所有现有的具有
Foo1
名称的方法的指针?像这样的东西:

template <typename T>
void assign(){          
  d = if exist(T for double) ? &(T for double) : nullptr;
  i = if exist(T for int) ? &(T for int) : nullptr;
  f = if exist(T for float) ? &(T for float) : nullptr;
}
c++ templates function-pointers
© www.soinside.com 2019 - 2024. All rights reserved.