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);
}
}
}
我明白這個錯誤。 所以問題是如何引用模板默認參數或其他方式來實現此設計模式的當前類類型?