您必须在某个地方告诉编译器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);
}
现在,假设您的所有函数共享相同的名称模式—— foo
fordouble
和sfoo
for 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
而不是add
or来调用它adds
。
这也可以通过其他形式的文本代码生成来完成。