默认模板模板参数的语法
我写了一个类似列表的模板类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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须写:
You have to write: