编译时类型名到函数名的映射

typename to function name mapping at compile time

我想要实现的东西与这个post中的相似:Select function name based on template parameter

在c库中,为了处理不同的数值类型(float、double、complex-float、complex-double),会有不同名称的函数。

在创建这样一个 c 库的包装器时,我想找出一种方法来实现重载效果并为所有数字类型使用相同的函数名。现在我使用模板专业化:

#include <iostream>

float saxpy(float x) {
    return (1-x);
}

double daxpy(double x) {
    return (1+x);
}

template <typename T> 
T axpy(T x);

template<>
inline float axpy<float>(float x) {
    return saxpy(x);
}

template<>
inline double axpy<double>(double x) {
    return daxpy(x);
}


int main() {

    auto z0 = axpy(1.0f);
    auto z1 = axpy(1.0);

    std::cout << z0 << " " << z1 << std::endl;

    return 0;
}

还有另一种使用类型特征的方法:

#include <type_traits>
#include <iostream>

float saxpy(float x) {
    return (1-x);
}

double daxpy(double x) {
    return (1+x);
}


struct saxpy_traits {
    static float feval(float x) { return saxpy(x); }
};

struct daxpy_traits {
    static double feval(double x) { return daxpy(x); }
};


template <typename T>
struct axpy_traits {
    typedef typename std::conditional<std::is_same<T, float>::value, saxpy_traits, daxpy_traits>::type Func;
    inline static T feval(T x) { 
        return Func::feval(x); 
    }
};

template<typename T>
inline T axpy(T x) {
    return axpy_traits<T>::feval(x);
}


int main() {

    auto z0 = axpy(1.0f);
    auto z1 = axpy(1.0);

    std::cout << z0 << " " << z1 << std::endl;

    return 0;
}

有没有更优雅的方式?谢谢。

由于我不知道 "elegant" 到底是什么,我提出两种替代方法:

float saxpy(float x)
{
  return 1.0f - x;
}



double daxpy(double x)
{
  return 1.0 + x;
}



namespace v1 {

float axpy(float x) { return saxpy(x); }

double axpy(double x) { return daxpy(x); }

} // namespace v1



namespace v2 {

struct Axpy
{
  float operator()(float x) const { return saxpy(x); }

  double operator()(double x) const { return daxpy(x); }
};

static constexpr Axpy axpy;

} // namespace v2

第一个版本简单地定义了两个重载函数。这类似于标准库采用的方法,例如std::sqrt.

第二个版本定义了一个具有重载 operator() 的函数对象。此版本允许代码如

  std::vector<float> fs { 1.f, 2.f, 3.f, 4.f };
  std::vector<float> rs(fs.size());
  std::transform(fs.begin(), fs.end(), rs.begin(), v2::axpy);

第一个版本无法编译。