CPP模板成员函数专门化 [英] CPP templated member function specialization
问题描述
模板< ; 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 *> i,j);
}
template< bool useCache>
friend const typename T :: scalar abstractWaveletSpecialization< Derived,T,useCache> :: moment(const Derived * const,const int i,const int j);
protected:
//返回第i个缩放函数的第j个时刻
template< bool useCache>
inline const typename T :: scalar momentImpl(const int j,const int i)const {
[...]
} // momentImpl
}
实际的专门化发生在额外的abstractWaveletSpecialization结构中:
模板< class Derived,class T,bool useCache>
struct abstractWaveletSpecialization {
inline static const typename T :: scalar moment(const Derived * const,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,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(): $ b
$ b 错误:无效的操作数类型'<未解析的重载函数类型>'和'bool'二进制操作符<'
但编译器不会在非专门的abstractWaveletSpecialization结构中调用momentImpl。
我的方法在C ++中被禁止了吗?
解决方案你可以尝试 that-> template momentImpl< ; true>(i,j);
请问?
这是一种告诉编译器嘿,后面的事情 - >是一个模板调用的方式。
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?
解决方案 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"
这篇关于CPP模板成员函数专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!