使用可变参数CRTP的智能指针工具包 [英] Smart pointer toolkit using variadic CRTP

查看:71
本文介绍了使用可变参数CRTP的智能指针工具包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将要设计和实现一种智能指针工具包-一组用于定义各种类型的智能指针的类,例如unique_ptr,intrusive_ptr,shared_ptr,observing_ptr,tagd_ptr等.我没有可用的c ++库.我的想法是避免代码重复,使其遵循优雅的设计原则.让我描述一下我的想法.

I am about to design and implement a kind of smart pointer toolkit - a set of classes to define various types of smart pointers like unique_ptr, intrusive_ptr, shared_ptr, observing_ptr, tagged_ptr etc. Just to mention I am working in freestanding environment where I have no c++ library available. My intersion is to avoid code duplications, make it follow an elegant design principle. Let me describe my thoughts in there.

设计注意事项:我想使用可变的CRTP方法来混合所需的指针功能,特征.对于每个功能集,都应该有一个特征类,例如:

Design Considerations: I wanna use variadic CRTP approach to mixin the desired pointer features, the traits. For every feature set there shall be once trait class like:

/* Functions to support move semantics like move constructor, move assignment operator */
template<typename DERIVED>
class MoveablePointer { ... };

/* Functions to manage pointee lifetime - using the deleter class */
/* EDIT: template<typename DERIVED, typename DELETER = DefaultDeleter<typename DERIVED::element_type>> */
template<typename DERIVED, typename DELETER> /* <-------- Default removed here as EDIT */
class Owning { 
public:
    using deleter_type = DELETER;
    
    /* ... */
};

/* Functions to use least significant bits effectively unused due to alignment to hold additional payload information */
template<typename DERIVED, typename TAGS, size_t ALIGNMENT>
class Tagged { ... };

,然后使用可变的CRTP模式将特征混合到一个智能指针类中:

and then to mixin the traits into one smart pointer class using variadic CRTP pattern:

template<template<typename> class... TRAITS>
struct make_traits {
    template<typename DERIVED>
    struct Traits : public TRAITS<DERIVED>... {};
};
template<typename T, template<typename> class POINTER_TRAITS>
class SmartPointer : public POINTER_TRAITS<SmartPointer<T, POINTER_TRAITS>> {
public:
    using pointer = T*;
    using element_type = T;

    /* ... */
};

template<typename T, typename DELETER = DefaultDeleter<T>>
using unique_ptr = SmartPointer<T, make_traits<MoveablePointer, Owning>>;

template<typename T>
using observer_ptr = SmartPointer<T, make_traits<Observing, NonOwning, Copyable, Moveable>>;

/* ... and to continue defining type aliases to cover various smart pointer variations... */

基本上,一旦特定特征具有相同的模板参数集(仅 template< typename DERIVED> ),此想法就可以奏效.但是如上所述,情况并非如此,例如 Tagged 特性或 Owning 特性需要更多模板参数,例如 ALIGNMENT 分别删除.

Basically this idea seem to work once the particular traits have the same set of template arguments (just template<typename DERIVED>). But as sampled above, it is not the case as for instance the Tagged trait or Owning trait require more template parameters like ALIGNMENT or DELETER respectively.

所以我的问题是-如何更改make_traits<>的实现;和SmartPointer<>类模板以支持可变特征的模板参数?

So my question is - how to change the implementation of make_traits<> and SmartPointer<> class templates to support variadic trait's template arguments?

非常感谢任何愿意帮助我的人!马丁

Many thanks to anyone willing to help me! Martin

编辑:随着讨论在答案下方出现,代码进行了调整...

EDIT: Code adjusted as the discussion flows below the answer...

推荐答案

您可以使用可变参数模板:

You might use variadic template:

template<template<typename, typename...> class... TRAITS>
//                        ^^^^^^^^^^^^^
struct make_traits {
    template<typename DERIVED>
    struct Traits : public TRAITS<DERIVED>... {};
};
template<typename T, template<typename> class POINTER_TRAITS>
class SmartPointer : public POINTER_TRAITS<SmartPointer<T, POINTER_TRAITS>> {
public:
    using pointer = T*;
    using element_type = T;

    /* ... */
};

template<typename T, typename DELETER = DefaultDeleter<T>>
using unique_ptr = SmartPointer<T, make_traits<MoveablePointer, Owning>::template Traits>;

template<typename T>
using observer_ptr = SmartPointer<T, make_traits<Observing, NonOwning, Copyable, MoveablePointer>::template Traits>;

演示

这篇关于使用可变参数CRTP的智能指针工具包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆