views:

57

answers:

1

Hi,

I'm trying to specialize the member function moment() only (not the hole class) like this:

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:

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:

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.

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

+1  A: 

Can you try that->template momentImpl<true>(i, j); please ? It's a way to tell the compiler "Hey, the thing after -> is a templated call"

Scharron
This syntax is looking weird. But is works!
Manuel
Yeah totally unusual. But that's what it takes when using advanced templated c++ :-)
Scharron
@Manual: The problem is that `A->B<C`, with `A` and `B` being templates, when the compiler first encounters these templates (no template parameters substituted yet) the compiler doesn't what `B` is, because that depends on the type of `A`, which isn't instantiated yet. (Think of specializations of `A` which might define `B` to be something different than in the base template.) If `B` turns out to be a member template, then `A->B<C` should be the beginning of `A->B<C>`. If it is an object, then `A->B<C` should be check whether `B` is less than `C`. That's why you need to hint the compiler.
sbi