指针作为模板参数?
我有一个容器类,我们称之为容器类,
template <class T> CVector { ... }
当 T 是指针类型时,我想对这个类做一些不同的事情,例如:
template <class T*> CVector< SomeWrapperClass<T> >;
其中 SomeWrapperClass 期望将所指向事物的类型作为其参数。 不幸的是,这种语法不太有效,经过一番挖掘,我还没有找到一个好的方法来让这样的东西工作。
为什么要这样做呢? 我想改变,在一个非常大的应用程序中,当我们的一些容器专门处理的类型是指针而不是指针时,它们的工作方式 - 理想情况下,我想在不更改〜1,000个位置的情况下做到这一点在代码中,有诸如 CVector
vs CVector
之类的东西 - 并且玩具有部分专业化的游戏似乎是正确的方法。
我这里是不是快崩溃了?
I have a container class, we'll call it
template <class T> CVector { ... }
I want to do something different with this class when T is a pointer type, e.g. something along the lines of:
template <class T*> CVector< SomeWrapperClass<T> >;
where SomeWrapperClass is expecting the type of the pointed to thing as its parameter. Unfortunately, this syntax doesn't quite work and with some digging, I haven't found a good way to get something like this working.
Why do it this way? I want to change, in a very large app, how some of our containers work when the type they're specialized on is a pointer vs. not a pointer - and ideally, i'd like to do it without changing the ~1,000 places in the code where there are things like CVector<Object*>
vs CVector<int>
or some such - and playing games with partial specializations seemed to be the way to go.
Am I on crack here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果我理解正确,这可能会满足您的要求:
它添加了一个额外的继承层来欺骗 CVector成为
CVector
CVector。 SomeWrapperClass; >
。 如果您需要添加其他方法以确保T*
的预期接口与SomeWrapperClass
提供的接口之间完全兼容,这也可能很有用。If I understand you correctly, this might do what you want:
It adds an additional layer of inheritance to trick
CVector<T*>
into being aCVector< SomeWrapperClass<T> >
. This might also be useful in case you need to add additional methods to ensure full compatibility between the expected interface forT*
and the provided interface forSomeWrapperClass<T>
.这在 C++ 中工作得很好......
This works just fine in C++...
我不认为你可以使用你描述的语法来专门化一个类......我不知道这可能如何工作。 您可以做的是专门化指针类,并使用原始指针周围的包装类重新实现其内部。 我不确定它是否有帮助,但是这篇文章描述了指针的专用模板。
I don't think you can specialize a class using the syntax you describe... I don't know how that could possibly work. What you can do is specialize the class for pointers and re-implement its guts using the wrapper class around the raw pointers. I'm not sure if it will help, but this article describes specializing templates for pointers.
Boost 类型特征库 可以帮助您实现这一目标。 查看 is_pointer 类型特征。
The Boost type traits library can help you achieve this. Check out the is_pointer type trait.
我不认为模板那么灵活。
一种非常强力的方法是专门针对所有指针类型......这解决了使用模板的问题。
您是否可以有一个仅用于指针向量的不同 CVector 类?
I don't think templates are quite that flexible.
A very brute force approach would be to specialize for all of your pointer types...which defeats the problem of using templates.
Could you have a different CVector class that is used only for vectors of pointers?
我同意rlbond的回答。 我对其进行了一些修改以满足您的需要。 CVector 可以是 CVector 本身的派生类。 然后您可以为其使用不同的成员和函数。
I agree with rlbond's answer. I have modified it a little bit to suit your need. CVector can be a derived class of the CVector itself. You can then use different members and functions for it.