3
我们有一个reference_counted
模板和默认default_deallocator
类如下:如何在默认模板参数中引用自我类型?
template <class T>
class default_deallocator {
void operator()(T* obj) {
delete obj;
}
};
template <class T>
class reference_counted: T
{
public:
void retain() {ref_count++;}
void release() {
ref_count --;
if (ref_count == 0) {
delete this;
}
}
}
我们要添加的释放器为reference_counted
类。但是我们不知道如何编写默认模板参数,因为编译器会抱怨递归类型引用。
//VS2015 says: fatal error C1202: recursive type or function dependency context too complex
template <class T, class D = default_deallocator<reference_counted<T>>> <---
class reference_counted: T
{
public:
void retain() {ref_count++;}
void release() {
ref_count --;
if (ref_count == 0) {
D deallocator;
deallocator.operator()(this);
}
}
}
我明白这个错误。 所以问题是如何引用模板默认参数或其他方式来实现此设计模式的当前类类型?