重载函数候选匹配问题

时间:2013-08-02 20:30:44

标签: c++ templates overloading typeid

我不确定我理解这里发生的事情的细微差别,并希望得到解释。

我从模板化的包装器lapack_gesvd_nothrow调用了几个重载函数。从那里,我打电话给这样的个人fxns:

inline void lapack_gesvd(char *jobu, char *jobvt, 
  int *m, int *n, 
  float *a, int *lda,   
  float *s, 
  float *u, int *ldu,  
  float *vt, int *ldvt,
  float *work,  int *lwork, 
  int *info) {
  sgesvd_(jobu, jobvt, m, n, 
        a, lda, s, u, ldu, 
        vt, ldvt, work, lwork, 
        info);
}
inline void lapack_gesvd(char *jobu, char *jobvt, 
  int *m, int *n, 
  nm::Complex64 *a, int *lda,   
  nm::Complex64 *s, 
  nm::Complex64 *u, int *ldu,  
  nm::Complex64 *vt, int *ldvt,
  nm::Complex64 *work,  int *lwork, float *rwork,
  int *info) {
  cgesvd_(jobu, jobvt, m, n,
      a, lda, s, u, ldu,
      vt, ldvt, work, lwork,
      rwork, info);
}

直到我宣布第二种类型的重载(我已经重载浮动和双重,没有问题),但是现在它抛出了一些错误,似乎不能很好地计算我的论点。

我从一个接受参数的函数中调用它:

template <typename DType, typename CType>
static int lapack_gesvd_nothrow(char *jobu, char *jobvt, 
  int m, int n, 
  void *a, int lda,   
  void *s, 
  void *u, int ldu,  
  void *vt, int ldvt,
  void *work,  int lwork,
  int info, void *rwork) {
....
DType* UPCASE = reinterpret_cast<DType*>(lowercase);
....

  if (typeid(DType) == typeid(CType)) {
    lapack_gesvd(jobu, jobvt, &m, &n, A, &lda, S, U, &ldu, VT, &ldvt, WORK, &lwork, &info);
  } else {
    CType* RWORK = reinterpret_cast<CType*>(rwork);
    lapack_gesvd(jobu, jobvt, &m, &n, A, &lda, S, U, &ldu, VT, &ldvt, WORK, &lwork, RWORK, &info);
  }

我实际上只在两者之间做reinterpret_casts

这是它显然正在寻找的fxn:

error: no matching function for call to ‘lapack_gesvd(char*&, char*&, int*, int*, float*&, int*, float*&, float*&, int*, float*&, int*, float*&, int*, float*&, int*)

以下是候选人匹配:

candidates are:
note: void nm::math::lapack_gesvd(char*, char*, int*, int*, float*, int*, float*, float*, int*, float*, int*, float*, int*, int*) 
note:    candidate expects 14 arguments, 15 provided
void nm::math::lapack_gesvd(char*, char*, int*, int*, double*, int*, double*, double*, int*, double*, int*, double*, int*, int*) 
note:   candidate expects 14 arguments, 15 provided
void nm::math::lapack_gesvd(char*, char*, int*, int*, nm::Complex64*, int*, nm::Complex64*, nm::Complex64*, int*, nm::Complex64*, int*, nm::Complex64*, int*, float*, int*)
note:   no known conversion for argument 5 from ‘float*’ to ‘nm::Complex64* {aka nm::Complex<float>*}’

我很困惑为什么解除引用现在出现,当它显示需要error: invalid conversion from 'int' to 'int*'没有它,并且在最近的重载之前需要。

您的解释和解决方案将非常感谢!谢谢!

修改

可能归结为:

如果我拨打lapack_gesvd_nothrow<float, float>(...)然后执行typeid比较if (typeid(DType) == typeid(CType)) ...我会得到预期的答案吗?现在,它似乎不是这样。如何正确检查模板类型以进行比较?

1 个答案:

答案 0 :(得分:1)

<强> TL; DR

你这样做:

if (false) {
    // some language rule violation here
} else {
    // correct code here
}

if-else的两边都需要编译。

<强>解决方案

您可以部分专门化模板。

template <class U, class V>
void foo(...){
    //assume U and V are different
}

template <class U>
void foo<U, U>(...){
    //assume both types are the same
}

为什么会这样?

编译器替换编译时指定的类型。这种方式如果你初始化,它将改变if with float

lapack_gesvd_nothrow<float, float>(...)

if (typeid(float) == typeid(float))

这样,代码最终会像这样:

if (typeid(float) == typeid(float)){
    lapack_gesvd(jobu, jobvt, &m, &n, A, &lda, S, U, &ldu, VT, &ldvt, WORK, &lwork, &info);
} else {
    CType* RWORK = reinterpret_cast<CType*>(rwork);
    lapack_gesvd(jobu, jobvt, &m, &n, A, &lda, S, U, &ldu, VT, &ldvt, WORK, &lwork, RWORK, &info);
}

编译器会看到A的类型是float。 if-else语句的第一部分是正确的。虽然,在第二部分不是。以下是编译器将如何看到它的示例:

void foo(float, float){};
void foo(int, int, int){};
template <class U>(){
...
    U a, b, c;
    if (...)
        foo(a, b);
    else
        foo(a, b, c);
...
}
//Will be changed to
float a, b, c;
if (...)
    foo(a, b);
else
    foo(a, b, c);

导致else部分出现编译错误,因为没有foo(float,float,float)。