STL 兼容容器的样板类型定义
在标准库或 Boost 中是否存在某种实用程序基类,用于使用所需的 typedef(size_type、value_type 等)填充自定义 STL 兼容序列。我正在考虑类似 boost::iterator_facade,但对于容器。
我本来打算自己卷起来,但想确保这样的东西还不存在。
更新:
这是我提出的实用程序基类,以防有人发现它有用:
template <class C>
class ContainerAdapter
{
public:
typedef C::value_type value_type;
typedef C::reference reference;
typedef C::const_reference const_reference;
typedef C::const_iterator iterator;
typedef C::const_iterator const_iterator;
typedef C::difference_type difference_type;
typedef C::size_type size_type;
protected:
typedef C::container_type;
};
// Usage
class MyCustomContainer : public ContainerAdapter< std::vector<int> >
{
...
};
ContainerAdapter 只是“回显”自定义容器的底层容器的嵌套 typedef。真的没什么。
Is there, within the standard library or Boost, some kind of utility base class for populating a custom STL-compatible Sequence with the required typedefs (size_type, value_type, etc...). I'm thinking of something like boost::iterator_facade, but for containers.
I was going to roll-up my own, but wanted to make sure such a thing didn't already exist.
UPDATE:
This is the utility base class I came up with, in case anybody finds it useful:
template <class C>
class ContainerAdapter
{
public:
typedef C::value_type value_type;
typedef C::reference reference;
typedef C::const_reference const_reference;
typedef C::const_iterator iterator;
typedef C::const_iterator const_iterator;
typedef C::difference_type difference_type;
typedef C::size_type size_type;
protected:
typedef C::container_type;
};
// Usage
class MyCustomContainer : public ContainerAdapter< std::vector<int> >
{
...
};
ContainerAdapter simply "echoes" the nested typedefs of a custom container's underlying container. There's nothing to it, really.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使它确实存在,您仍然必须
typedef typename base::size_type size_type
。看来你不会有太大收获。
even if it does exist, you still have to
typedef typename base::size_type size_type
.does not seem you would gain much.