简体   繁体   English

CPP模板化成员函数专业化

[英]CPP templated member function specialization

I'm trying to specialize the member function moment() only (not the hole class) like this: 我正在尝试专门化成员函数moment()(不是孔类),如下所示:

template<class Derived, class T>
class AbstractWavelet {
public:
  [...]

  template<bool useCache>
  const typename T::scalar moment(const int i, const int j) const {
    return abstractWaveletSpecialization<Derived, T, useCache>::moment(static_cast<const Derived*>(this), i, j);
  }
  template<bool useCache>
  friend const typename T::scalar abstractWaveletSpecialization<Derived, T, useCache>::moment(const Derived* const that, const int i, const int j);

protected:
  // returns the jth moment of the ith scaling function
  template<bool useCache>
  inline const typename T::scalar momentImpl(const int j, const int i) const {
    [...]
  } // momentImpl
};

The actual specialization happens in an extra abstractWaveletSpecialization struct: 实际的特化在一个额外的abstractWaveletSpecialization结构中发生:

template<class Derived, class T, bool useCache>
struct abstractWaveletSpecialization {
  inline static const typename T::scalar moment(const Derived* const that, const int i, const int j) {
    return that->momentImpl<useCache>(i,j);
  }
};


template<class Derived, class T> 
struct abstractWaveletSpecialization<Derived, T, true> {

  typedef const std::pair<const int, const int> momentCacheKey;
  typedef std::map<momentCacheKey,
               const typename T::scalar> momentCacheType;
  static momentCacheType momentCache;


  inline static const typename T::scalar moment(const Derived* const that, const int i, const int j) {
    momentCacheKey cacheKey(i,j);
    typename momentCacheType::iterator idx = momentCache.find(cacheKey);

    if (idx == momentCache.end())
      return that->momentImpl<true>(i, j);  // COMPILE ERROR HERE
    else
      return momentCache[cacheKey];
  }
};

The problem is that I cannot call momentImpl() in the specialized abstractWaveletSpecialization struct: 问题是我不能在专门的abstractWaveletSpecialization结构中调用momentImpl():

error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘bool’ to binary ‘operator<’

But the compiler does not complain about the call of momentImpl in the non-specialized abstractWaveletSpecialization struct. 但是编译器并没有抱怨在非专用的abstractWaveletSpecialization结构中调用momentImpl。

Is my approach forbidden in C++? 我的方法是否在C ++中被禁止? Or is there any way to make this work? 或者有什么方法可以使这项工作?

Can you try that->template momentImpl<true>(i, j); 你能试试吗that->template momentImpl<true>(i, j); please ? 请 ? It's a way to tell the compiler "Hey, the thing after -> is a templated call" 这是一种告诉编译器的方法“嘿,事后 - >是一个模板化的调用”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM