在编译时使用reinterpret_cast检查继承 [英] Using reinterpret_cast to check inheritance at compile time
问题描述
关于此问题:何时使用reinterpret_cast?
我找到了sth。像这样:
I found sth. like this:
template<typename T> bool addModuleFactoryToViewingFactory(ViewingPackage::ViewingFactory* pViewingFactory)
{
static_cast<ModuleFactory*>(reinterpret_cast<T*>(0)); // Inheritance compile time check
...
}
这是一个检查在编译时是否可以将 T
转换为 ModuleFactory
的好方法?
我的意思是,检查程序员是否把有效的东西放入 addModuleFactoryToViewingFactory< T>(...)的
< code>
这是好的,好还是唯一的方法?
Is this a good way to check whether T
can be casted to ModuleFactory
at compile time?
I mean, to check if the programmer put valid stuff into the <>
of addModuleFactoryToViewingFactory<T>(...)
Is this okay, good or maybe the only way?
问候
推荐答案
您试图解决一个不需要解决的问题。从C ++ 11开始,我们有Type Traits,它允许我们在Template Metaprogramming中明确地检查这样的事情。
You're trying to solve a problem that doesn't need to be solved. Since C++11, we have Type Traits that allow us to check things like this explicitly in Template Metaprogramming.
例如,is_base_of
For example, is_base_of
http://en.cppreference.com/w/cpp/types/is_base_of
这篇关于在编译时使用reinterpret_cast检查继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!