我试图专门化成员函数 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
};
实际的特化发生在一个额外的 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];
}
};
问题是我不能在专门的 abstractWaveletSpecialization 结构中调用 momentImpl() :
error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘bool’ to binary ‘operator<’
但是编译器不会抱怨在非专门的 abstractWaveletSpecialization 结构中调用 momentImpl。
我的方法在 C++ 中是否被禁止?或者有什么办法可以使这项工作?