【问题标题】:C++: Partial template specializationC++:部分模板特化
【发布时间】:2010-07-09 20:11:01
【问题描述】:

我没有获得部分模板专业化。 我的班级是这样的:

template<typename tVector, int A>
class DaubechiesWavelet : public AbstractWavelet<tVector> { // line 14
public:
  static inline const tVector waveletCoeff() {
    tVector result( 2*A );
    tVector sc = scalingCoeff();

    for(int i = 0; i < 2*A; ++i) {
      result(i) = pow(-1, i) * sc(2*A - 1 - i);
    }
    return result;
  }

  static inline const tVector& scalingCoeff();
};

template<typename tVector>
inline const tVector& DaubechiesWavelet<tVector, 1>::scalingCoeff() { // line 30
  return tVector({ 1, 1 });
}

gcc 输出的错误是:

line 30: error: invalid use of incomplete type ‘class numerics::wavelets::DaubechiesWavelet<tVector, 1>’
line 14: error: declaration of ‘class numerics::wavelets::DaubechiesWavelet<tVector, 1>’

我尝试了几种解决方案,但都没有奏效。 有人给我提示吗?

【问题讨论】:

  • result(i) ?那不应该是result[i]吗?
  • 我用的是boost的ublas,所以你可以使用()操作符

标签: c++ templates partial-specialization


【解决方案1】:
template<typename tVector>
inline const tVector& DaubechiesWavelet<tVector, 1>::scalingCoeff() { // line 30
  return tVector({ 1, 1 });
}

这是一个部分特化成员的定义,其定义如下

template<typename tVector>
class DaubechiesWavelet<tVector, 1> {
  /* ... */
  const tVector& scalingCoeff();
  /* ... */
};

它不是主模板“DaubechiesWavelet”的成员“scalingCoeff”的特化。这样的特化需要传递所有参数的值,而您的特化不这样做。为了做你想做的事,你可以使用重载

template<typename tVector, int A>
class DaubechiesWavelet : public AbstractWavelet<tVector> { // line 14
  template<typename T, int I> struct Params { };

public:
  static inline const tVector waveletCoeff() {
    tVector result( 2*A );
    tVector sc = scalingCoeff();

    for(int i = 0; i < 2*A; ++i) {
      result(i) = pow(-1, i) * sc(2*A - 1 - i);
    }
    return result;
  }

  static inline const tVector& scalingCoeff() {
    return scalingCoeffImpl(Params<tVector, A>());
  }

private:
  template<typename tVector1, int A1>
  static inline const tVector& scalingCoeffImpl(Params<tVector1, A1>) {
    /* generic impl ... */
  }

  template<typename tVector1>
  static inline const tVector& scalingCoeffImpl(Params<tVector1, 1>) {
    return tVector({ 1, 1 });
  }
};

请注意,您使用的初始化语法仅适用于 C++0x。

【讨论】:

    【解决方案2】:

    我没有看到专业化的课程。您必须专门化该类,并在其中专门化方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多