【问题标题】:C++ Template Meta Programming: Inheritance from template template parameterC++ 模板元编程:从模板模板参数继承
【发布时间】:2018-11-11 18:27:34
【问题描述】:
#include <type_traits>

template <typename T1, typename T2, typename is_allocated>
struct mutable_storage {};

template <
    template<typename, typename, typename> class storage_t,
    typename T2           = void,
    typename is_allocated = std::false_type
>
class Buffer : storage_t<Buffer<storage_t,void,void>, T2, is_allocated>
{};



int main() {
    typedef Buffer<mutable_storage> example_buffer;
}

此代码可以编译(至少使用 C++14 之后的 GNU GCC 编译器)。但是,我不喜欢使用的语法

class Buffer : storage_t<Buffer<storage_t,void,void>, T2, is_allocated>

由于它不应该要求 Buffer 被专门化:我希望 Buffer 被识别为模板模板参数,例如:

class Buffer : storage_t<Buffer, T2, is_allocated>

然后我希望 mutable_storage 结构能够识别模板专业化,例如

template <typename T2, typename is_allocated>
struct mutable_storage<Buffer, T2, is_allocated> { ... };

(当然不允许,因为“Buffer”不是一种类型,所以也应该改变)。 但是它现在使用的方式,能够专注于类型 缓冲区感觉有点讨厌。使用 typedef,例如

 typedef Buffer<storage_t, void, void> Buffer_Policy

也觉得有点恶心。我正在寻找一种更清洁的方式。我试图制作一个模板模板模板参数,但这会导致模板参数中的额外模板无限流动(我不确切知道 template<...> 是如何工作的,所以也许是这样?),作为Buffer 继承自需要另一个 Buffer 才能声明 storage_t 的东西。我也尝试过使用隐式类,即 inner_storage_t。这也没有导致成功。有没有人有建议以使程序更清洁?顺便说一句,如果您发现任何其他错误或效率低下,请随时提及。感谢您的阅读以及您的帮助。

【问题讨论】:

  • 这确实看起来很尴尬。在不知道存储类型对 T1 参数的预期作用的情况下提出替代方案有点困难。你能详细说明一下吗?
  • 非常感谢您的反应!它实际上仅用于模板特化,以定义如何存储类型 T1。我发现我在创建这篇文章时犯了一个小错误,我现在将对其进行编辑,但是为了专业化: template struct mutable_storage 应该是 template struct mutable_storage。给您带来的不便,我深表歉意!

标签: c++ templates template-meta-programming policy policy-based-design


【解决方案1】:

由于T1 仅用于模板专业化选择,因此您实际上不必使用Buffer 本身。您可以改用标签类型。

将其隐藏在命名空间中还可以避免标签污染封闭命名空间的其余部分。

#include <type_traits>

template <typename T1, typename T2, typename is_allocated>
struct mutable_storage {};

namespace storage_tags {
  struct Buffer_T {};
}

template <
    template<typename, typename, typename> class storage_t,
    typename T2           = void,
    typename is_allocated = std::false_type
>
class Buffer : public storage_t<storage_tags::Buffer_T, T2, is_allocated> { 

};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 2014-07-15
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多