如何在默认模板参数中引用自身类型?

How to reference self type in default template arguments?

我们有一个 reference_counted 模板和一个默认 default_deallocator class 如下:

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 class 添加释放器。但是我们不知道如何写默认模板参数,因为编译器会抱怨递归类型引用。

//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);
        }
   }
}

我理解这个错误。 所以问题是如何在模板默认参数中引用当前 class 类型或其他方式来实现此设计模式?

您可以使用更高级的类型 (a "template template parameter"):

template <class T, template <typename...> class D = default_deallocator>  
class reference_counted: T
{
public:
   void retain() {}
   void release() {

        D<reference_counted<T, D>> deallocator;
        deallocator(this);

   }
};

live example on wandbox

template <class T, class D_in = void>
class reference_counted: T
{
public:
  using D = std::conditional_t<
    std::is_same_v<D_in, void>,
    default_deallocator<reference_counted>,
    D_in
  >;
  void retain() {ref_count++;}
  void release() {
    ref_count --;
    if (ref_count == 0) {
        D deallocator;
        deallocator.operator()(this);
    }
  }
};