3

我的问题如下:我有一个 C 库,其中包含每个函数的多个版本,具体取决于它们使用的数据类型,例如:

void add(double *a, double *b, double *c);

void sadd(float *a, float *b, float *c);

现在,拥有一个外部 C++ 模板函数,我希望能够执行以下操作:

template<class T>
void myfunc(/*params*/)
{
 // Obtain a,b,c of type T* from params

/*
 If T is double call add(a,b,c);
 else if T is float call sadd(a,b,c).
*/ 
}

我知道它可以通过专门的模板功能来完成,例如:

template<>
void myfunc<double>(/*params*/)
{
  // Obtain a,b,c of type double* from params
  add(a,b,c);
}

等等,但这并不是一个真正的选择,因为引入模板化 C++ 函数的全部目的是减少代码重复,并且“// 从参数中获取 T* 类型的 a、b、c”部分可能非常长。

这个问题有简单的解决方案吗?

谢谢

兹德内克

4

2 回答 2

10

定义重载的 C++ 转发器:

inline void forward_add(double *a, double *b, double *c) { add( a, b, c ); }
inline void forward_add(float *a, float *b, float *c) { sadd( a, b, c ); }

template<class T>
void myfunc(/*params*/)
{
   // Obtain a,b,c of type T* from params
   forward_add( a, b, c );
}
于 2013-04-18T17:11:37.313 回答
1

您必须在某个地方告诉编译器sadd并且add是相关的。

一种方法是特征类模板结构数学;

template<>
struct math<double> {
  static void add(double *a, double *b, double *c) {
    return ::add(a, b, c);
  }
};
template<>
struct math<float> {
  static void add(float*a, float*b, float*c) {
    return ::sadd(a, b, c);
  }
};

你在哪里使用它:

template<class T>
void myfunc(/*params*/)
{
  // Obtain a,b,c of type T* from params

  math<T>::add( a, b, c );
}

这具有将所有基于类型的重构放在一个位置的优点和缺点。

double另一种方法是创建具有和重载的独立 C++ 函数float。这具有允许您的代码分布在多个位置的优点和缺点。

void math_add( double* a, double* b, double* c ) {
  add(a,b,c);
}
void math_add( float* a, float* b, float* c ) {
  sadd(a,b,c);
}

现在,假设您的所有函数共享相同的名称模式—— foofordoublesfoofor float。在这种情况下,可以使用基于文本的代码生成来缓解上述一些“编写重载”代码。

这里唯一的问题是函数的签名可能会有所不同。如果只有少数几个,简单的宏就可以了:

#define MAKE_FUNCS( f ) \
  void CONCAT( math_, f ) ( double* a, double* b, double * c ) { \
    f ( a, b, c ); \
  } \
  void CONCAT( math_, f ) ( float* a, float* b, float* c ) { \
    CONCAT( f, s ) ( a, b, c ); \
  }

然后只需MAKE_FUNCS为您尝试以这种方式克隆的库中的每个函数发送垃圾邮件。

一个缺点(很多)是它只支持一组固定的签名。我们可以通过完美转发来解决这个问题,这是一种 C++11 技术:

#define MAKE_FUNCS( f ) \
  template< typename... Args >\
  auto f ( Args&&... args ) \
    -> decltype(::f ( std::forward<Args>(args)... )) \
  { \
    ::f ( std::forward<Args>(args)... ); \
  } \
  template< typename... Args >\
  auto f ( Args&&... args ) \
    -> decltype(:: CONCAT( f, s ) ( std::forward<Args>(args)... )) \
  { \
    :: CONCAT( f, s ) ( std::forward<Args>(args)... ); \
  }

但这会遇到 SFINAE 和相同的签名问题。您可以通过显式表达式 SFINAE 来解决此问题:

#include <utility>
#include <type_traits>
#include <cstddef>
#include <iostream>

#define CONCAT2( a, b ) a##b
#define CONCAT( a, b ) CONCAT2(a,b)

// SFINAE helper boilerplate:
template<typename T> struct is_type:std::true_type {};
template<std::size_t n> struct secret_enum { enum class type {}; };
template<bool b, std::size_t n>
using EnableIf = typename std::enable_if< b, typename secret_enum<n>::type >::type;

// Macro that takes a srcF name and a dstF name and an integer N and
// forwards arguments matching dstF's signature.  An integer N must be
// passed in with a distinct value for each srcF of the same name:
#define FORWARD_FUNC( srcF, dstF, N ) \
template< typename... Args, \
  EnableIf< is_type< \
    decltype( dstF ( std::forward<Args>(std::declval<Args>())... )) \
  >::value , N >... > \
auto srcF ( Args&&... args ) \
  -> decltype(dstF ( std::forward<Args>(args)... )) \
{ \
  dstF ( std::forward<Args>(args)... ); \
}

#define MAKE_FUNCS( f ) \
  FORWARD_FUNC( f, ::f, 0 ) \
  FORWARD_FUNC( f, :: CONCAT( f, s ), 1 )

void add( double* a, double* b, double* c) {*a = *b+*c;}
void adds( float* a, float* b, float* c) {*a = *b+*c;}
namespace math {
  MAKE_FUNCS(add)
}
int main() {
  double a, b = 2, c = 3;
  float af, bf = 3, cf = 5;
  math::add( &a, &b, &c );
  math::add( &af, &bf, &cf );
  std::cout << a << "=" << b << "+" << c << "\n";
  std::cout << af << "=" << bf << "+" << cf << "\n";
}

但正如您所看到的,这变得相当迟钝,目前没有多少编译器可以处理这种级别的 C++11 主义。(我认为上面应该在 gcc 4.8 和 intel 的最新版本中编译,但不是 MSVC 或 clang 3.2)

现在您只需获取库中的每个函数,并创建一个包含一堆单行样板的头文件:

namespace mymath {
  MAKE_FUNCS( add )
  MAKE_FUNCS( sub )
  MAKE_FUNCS( chicken )
}
#undef MAKE_FUNCS

然后你通过说mymath::add而不是addor来调用它adds

这也可以通过其他形式的文本代码生成来完成。

于 2013-04-18T18:09:21.767 回答