C++专业化、type_of 或只是 typeid
我想知道在我的情况下使用什么更好以及为什么。首先我听说使用 RTTI (typeid) 不好。任何人都可以解释为什么吗?如果我确切地知道类型,在运行时比较它们有什么问题?此外,有没有如何使用 boost::type_of 的示例?我在强大的谷歌中没有发现任何搜索:)对我来说另一个解决方案是专业化,但我需要专业化至少 9 种新方法。这是我需要的一个例子:
我有这个类
template<typename A, typename B, typename C>
class CFoo
{
void foo()
{
// Some chunk of code depends on old A type
}
}
所以我需要检查 typeid(我听到的是不好的)并在示例中实现这 3 个实现,例如:
void foo()
{
if (typeid(A) == typeid(CSomeClass)
// Do this chunk of code related to A type
else
if (typeid(B) == typeid(CSomeClass)
// Do this chunk of code related to B type
else
if (typeid(C) == typeid(CSomeClass)
// Do this chunk of code related to C type
}
那么最好的解决方案是什么?我不想专门针对所有 A、B、C,因为每种类型都有 3 个专门化,所以我将获得 9 种方法或只是这个 typeid 检查。
I would like to know what is better to use in my situation and why. First of all I heard that using RTTI (typeid) is bad. Anyone could explain why? If I know exactly types what is wrong to compare them in a runtime? Furthermore is there any example how to use boost::type_of? I have found none searching through the mighty google :) Other solution for me is specialization, but I would neet to specialize at least 9 types of new method. Here is an example what I need:
I have this class
template<typename A, typename B, typename C>
class CFoo
{
void foo()
{
// Some chunk of code depends on old A type
}
}
So I need to rather check in typeid(what is I heard is BAD) and make these 3 realizations in example like:
void foo()
{
if (typeid(A) == typeid(CSomeClass)
// Do this chunk of code related to A type
else
if (typeid(B) == typeid(CSomeClass)
// Do this chunk of code related to B type
else
if (typeid(C) == typeid(CSomeClass)
// Do this chunk of code related to C type
}
So what is the best solution? I don't want to specialize for all A,B,C, because every type is has 3 specializations so I will get 9 methods or just this typeid check.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这很糟糕,因为
您通常所做的是将代码分解为单独的函数模板或可以专门化的类的静态成员函数。
坏:
更好:
干杯,s
It's bad because
What you normally do is factoring out the code into separate function templates or static member functions of classes that can be specialized.
Bad:
Better:
Cheers, s
一般来说,无需 RTTI 即可提出解决方案。它“可以”表明您没有正确考虑软件的设计。那很糟糕。有时,RTTI可能是一件好事。
尽管如此,你想做的事情还是有些奇怪。您是否不能创建一个临时模板,设计如下:
然后部分专门化您想要的功能,如下所示:
然后在上面的类定义中,您将执行诸如...
模板
之类的操作,这样它实际上只在如果正在执行部分专门化的模板,则调用“DoSomething”。
Well generally solutions can be come up with without RTTI. It "can" show you haven't thought the design of the software out properly. THAT is bad. Sometimes RTTI can be a good thing though.
None-the-less there IS something odd in what you want to do. Could you not create an interim template designed something like as follows:
then partially specialise for the functions you want to as follows:
Then in your class define above you would do something such as ...
template
This way it only actually does something in the "DoSomething" call if it is going through the partially specialised template.
问题在于您为每个专业化编写的代码块。
好处
第二种情况的
是,当您添加类 D 时,编译器会提醒您存在 foo_ 缺失的重载,您必须编写该重载。在第一个变体中可以忘记这一点。
The problem lies in the code chunks you write for every specialization.
It doesn't matter if you write (lengthwise)
or
The upside of the second case is, when you add a class D, you get reminded by the compiler that there is an overload for foo_ missing which you have to write. This can be forgotten in the first variant.
恐怕这从一开始就行不通。即使类型不是 CSomeClass,这些“代码块”也必须是可编译的。
我认为 type_of 也没有帮助(如果它与 C++0x 中的 auto 和 decltype 相同)。
我认为您可以将这三个块提取到单独的函数中,并为 CSomeClass 重载每个函数。 (编辑:哦,有
else if's
。那么你可能确实需要大量重载/专业化。这段代码是做什么用的?)Edit2:看起来你的代码希望做与以下相同的事情,其中 int 是特殊类型:
I'm afraid this is not going to work in the first place. Those "chunks of code" have to be compilable even if the type is not CSomeClass.
I don't think type_of is going to help either (if it is the same as auto and decltype in C++0x).
I think you could extract those three chunks into separate functions and overload each for CSomeClass. (Edit: oh there are
else if's
. Then you might indeed need lots of overloads/specialization. What is this code for?)Edit2: It appears that your code is hoping to do the equivalent of the following, where int is the special type:
我认为你的抽象在某些地方是错误的。
我会尝试重新定义A、B& C 就其需要公开的接口而言(C++ 中具有纯虚方法的抽象基类)。
模板基本上允许鸭子类型,但听起来 CFoo 对 AB 和 AB 知道得太多了。 C类。
typeid 不好是因为:
二进制文件,携带额外的
不应该的信息
必需的。
我建议的是重构:删除模板,而是为 A、B 和 B 定义接口。 C,并让 CFoo 采用这些接口。这将迫使您重构行为,以便 A、B 和C实际上是内聚类型。
I think you've got your abstractions wrong somewhere.
I would try redefining A, B & C in terms of interfaces they need to expose (abstract base classes in C++ with pure virtual methods).
Templating allows basically duck-typing, but it sounds like CFoo knows too much about the A B & C classes.
typeid is bad because:
binaries, carries around extra
information that shouldn't be
required.
What I would recommend is refactoring: remove the templating, instead define interfaces for A, B & C, and make CFoo take those interfaces. That will force you to refactor the behaviour so the A, B & C are actually cohesive types.