标记指针 CRTP 访问器 class

Tagged pointer CRTP accessor class

我即将实现标记指针,它使用未使用的地址位来保存有效负载。我正在寻找访问有效负载的有效方法。声明如下:

template<typename T, template<class> class PAYLOAD, DeleterType DELETER = DefaultDeleter<T>, size_t ALIGNMENT = alignof( T )>
class tagged_ptr;

它与 unique_ptr 共享删除器等基本原理。我面临的问题是使用 PAYLOAD 模板模板参数来定义访问器 class 来操作有效负载位. PAYLOAD 应该是一个 class 模板,只包含访问函数(getters/setters),没有数据。这个想法是使用 PAYLOAD 作为 CRTP base class of tagged_ptr 来授予对其私有数据的访问权限 - 指针本身(uintptr_t - tagged_ptr 持有的地址)。所以我尝试将其定义为:

template<typename T, template<class> class PAYLOAD, DeleterType DELETER = DefaultDeleter<T>, size_t ALIGNMENT = alignof( T )>
    class tagged_ptr : public PAYLOAD<tagged_ptr> { ... }

但它不编译报告:

error: type/value mismatch at argument 1 in template parameter list for 'template<class> class PAYLOAD'
[build]    15 |  class tagged_ptr : public PAYLOAD<tagged_ptr> {
[build]       |                                              ^

所以我的问题是-

how to implement a CRTP for derived class template?

在普通的 CRTP 中,模式是这样的

template<typename DERIVED>
class Base { /* use static casts to DERIVED to access its data */ }

class Derived : public Base<Derived> { ... }

表示 Derived 不是 class 模板,但在我的情况下,它是...那么如何解决呢?非常感谢您的帮助!

template<typename T, template<class> class PAYLOAD>
class tagged_ptr : public PAYLOAD<tagged_ptr<T,PAYLOAD>> {};

Live example.