【发布时间】:2014-05-06 08:35:52
【问题描述】:
在用 C++ 实现基于模板的工厂时,我创建了以下 allocator 函数来实例化给定的子类:
template<class ChildClass, class ParentClass>
ParentClass* allocator() {
ChildClass *child = new ChildClass();
ParentClass*parent = dynamic_cast<ParentClass*>(child);
if(NULL==parent) {
delete child;
return NULL;
}
return parent;
}
一切正常,但是当通过coverity 等静态代码分析工具运行代码时,delete child; 行被标记为逻辑死代码。
我进行运行时检查的原因是断言ChildClass 是从ParentClass 派生的。
现在我明白了,在模板扩展期间编译器已经知道ChildClass 是否派生自ParentClass,并且dynamic_cast 仅在运行时进行评估。
如果ChildClass 确实是从ParentClass 派生的,那么运行时检查逻辑上是死代码(在这种情况下,如果ChildClass 已成功分配,dynamic_cast 将始终返回non-NULL) .
但是有没有办法确保在编译时(模板扩展时)ChildClass 派生自 ParentClass?
afaik,模板和继承在 C++ 中是不相关的,但我可能遗漏了一些明显的东西。
限制
不幸的是,代码应该在较旧的编译器上编译(例如,Visual Studio 6 附带的 C++ 实现)排除了任何较新的扩展,例如 C++11-features
【问题讨论】:
-
如果你的编译器和库支持 C++11,你可以使用
std::is_base_of。 -
@JoachimPileborg 好点,但是我希望能够使用 VisualStudio-6 (原文如此!)对我的代码进行旧版编译,所以它甚至不是 @987654340 @
-
你应该在你的问题中清楚地说明这些限制。
-
@juanchopanza 感谢您的提示,更新了问题
-
如果无法分配内存,您当前的实现将在
new上抛出std::bad_alloc,而不是nullptr。如果你想在失败时获得nullptr,你应该使用new(std::nothrow) ChildClass();;)
标签: c++ templates inheritance dynamic-cast