CRTP 容器
我正在尝试一些模板编程,对此我很陌生。我想要实现的是一些包含 STL 容器的 CRTP 类。让 class A{};
作为(编译时)基类的示例,其中 class B{};
和 class C{}; 在编译时按照 CRTP 样式“派生”。
现在 B
和 C
都将包含容器。出于示例的目的,将其分别设为 std::vector
和 std::set
。现在,我想通过 begin()
和一个公开前向迭代器的 end()
函数来公开这些迭代器。但是,我不想公开 B
和 C
内部的确切容器是什么,我想为 A
定义这些函数,以便在调用时使用 B
和 C
的正确值。
这可能吗?现在我的计划是为 B
和 C
建立一个 Iterator
内部类,其中将包含(一个向量或一个根据情况设置)并将调用委托给它。然而,这似乎是很多重复的粘合代码,我怀疑有更好的选择。
我有几个问题:
如何声明
A
、B
和C
中的内部类,以便它能够很好地配合CRTP。我需要为A
、B
和C
复制它吗?它可以是A
中的空类,然后用专门的实现将它们屏蔽在B
和C
中吗?如何以更少的粘合和更少的重复来公开迭代器?
我不想与 boost 这样的外部库创建依赖关系,并且只想坚持使用 std。所以我必须自己实现我需要的任何额外内容。感谢您的所有帮助。
I am cutting my teeth at some template programming and I am very new to this. What I want to implement are a few CRTP classes that contain an STL container. Let class A{};
serve as an example for the (compile time) base class from which class B{};
and class C{};
are "derived" at compile time following the CRTP style.
Now both B
and C
will contain containers. For the purpose of the example let it be a std::vector
and a std::set
respectively. Now, I want to expose the iterators of these via a begin()
and an end()
function that exposes a forward iterator. However, I do not want to expose what is the exact container that is inside B
and C
and I want to define these functions for A
, so that at call time the correct one for B
and C
get used.
Is this possible ? Right now my plan is to have a Iterator
inner class for B
as well as C
that will contain the actual iterator of (a vector or a set as the case may be) and delegate the call to it. However this seems to be a lot of replicated glue code and I suspect there is a better option.
I have a couple of questions:
How do I declare the inner clases in
A
,B
andC
so that it plays well with CRTP. Do I need to replicate it forA
,B
andC
? Can it be an empty class inA
and I mask them inB
andC
with specialized implementations ?How can I expose the iterator with less glue and less duplication ?
I do not want to create dependencies with external libraries like boost and want to stick to std only. So I have to implement whatever extra I need myself. Thanks for all the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也通过 CRTP 公开迭代器:
如果您有更多类型要公开,则将特征类作为模板参数传递给
Base
:Expose the iterator too via CRTP:
If you have more types to expose, then pass a traits class as a template argument to
Base
:如果我理解正确的话,你正在寻找这样的东西。请注意,我制作了一些简单的构造函数只是为了说明它的工作原理。另外,你的
class A
是我的class TWrapperBase
,B
-TWrapperB
,C
- TWrapperC。另一件事,对于这个特定的示例,您实际上并不需要有两个派生类,但我假设您的类B
和C
有显着不同,以证明它在您的程序中是合理的。编辑:忘记在循环中增加lIterSet。
If I understood you corretcly, you are looking for something like this. Note, I made some simple constructor just to illustrate that it works. Also, your
class A
is mineclass TWrapperBase
,B
-TWrapperB
,C
-TWrapperC
. Another thing, you don't really need to have two derived classes for this particular example, but I assume your classesB
andC
are significantly different to justify it in your program.EDIT: Forgot to increment lIterSet in the loop.