CPP Template Feature Specialization

I am trying to specialize only a member member () (and not a class of holes) as follows:

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
};

Actual specialization occurs in the optional abstractWaveletSpecialization structure:

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 structure:

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

But the compiler does not complain about the call to momentImpl in the non-specialized structure abstractWaveletSpecialization.

Is my approach forbidden in C ++? Or is there a way to make this work?

+3
source share
1 answer

that->template momentImpl<true>(i, j);, ? ", → - "

+2

Source: https://habr.com/ru/post/1756536/


All Articles