默认模板模板参数的语法

发布于 2024-10-14 22:51:12 字数 1110 浏览 3 评论 0原文

我写了一个类似列表的模板类sll(单链表)。现在,我正在尝试将分配器插入其中。我有默认的分配器 allocator 和基于池的分配器 pool_allocator。这些是在 STL 分配器接口之后设计的,但我需要添加分配器将处理的元素数量(max_size)作为模板参数。所以,我做了以下事情。

enum  {Default_1 = 16};         // for example
template <typename T, size_t N = Default_1>
struct allocator {
};

enum  {Default_2 = 32};         // for example
template <typename T, size_t N = Default_2>
struct pool_allocator {
};

我想支持两种类型的客户端使用:

1. sll<int>  == implying ==> sll<int, allocator<int, Default_1> >

2. sll<int, pool_allocator<int, 4096> >

我遇到的困难是在 sll 模板类中指定默认分配器。最初我有

template<typename T, typename Allocator = allocator<T> > class sll { ...};

它的工作原理,但问题是,用户无法指定分配器的容量。

我尝试过

template<typename T, 
    typename Allocator = allocator< typename T, size_t N = Default_3> >
class sll { ... };

,但收到错误:

error: template argument 1 is invalid

我尝试了一些其他组合,但没有一个起作用。此时,我已经没有主意了,正在向 SO 社区寻求帮助。任何建议或指示表示赞赏。

I have written a list-like template class sll (Single Linked List). Now, I am trying to plugin an allocator to it. I have the default allocator, allocator, and a pool based allocator, pool_allocator. These are designed after STL allocator interface, but I need to add the number of elements that the allocator would handle (the max_size) as a template parameter. So, I have done the following.

enum  {Default_1 = 16};         // for example
template <typename T, size_t N = Default_1>
struct allocator {
};

enum  {Default_2 = 32};         // for example
template <typename T, size_t N = Default_2>
struct pool_allocator {
};

I want to support two kinds if usage by the client:

1. sll<int>  == implying ==> sll<int, allocator<int, Default_1> >

2. sll<int, pool_allocator<int, 4096> >

The difficulty I am having is specifying the default allocator in the sll template class. Initially I had

template<typename T, typename Allocator = allocator<T> > class sll { ...};

It works, but the problem is, user can;t specify the capacity of the allocator.

I tried

template<typename T, 
    typename Allocator = allocator< typename T, size_t N = Default_3> >
class sll { ... };

but I receive the error:

error: template argument 1 is invalid

I tried few other combinations, but none of them worked. At this point, I am out of ideas, and looking for help from the SO community. Any suggestions or pointers are appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许久 2024-10-21 22:51:12

你必须写:

template<typename T, 
    typename Allocator = allocator<T, Default_3> >
class sll { ... };

You have to write:

template<typename T, 
    typename Allocator = allocator<T, Default_3> >
class sll { ... };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文