cast to check inheritance at compile time
Regarding this question: When to use reinterpret_cast?
I found sth. like this:
template<typename T> bool addModuleFactoryToViewingFactory(ViewingPackage::ViewingFactory* pViewingFactory)
{
static_cast<ModuleFactory*>(reinterpret_cast<T*>(0)); // Inheritance compile time check
...
}
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?
Greetings
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.
For example, is_base_of
http://en.cppreference.com/w/cpp/types/is_base_of
链接地址: http://www.djcxy.com/p/73754.html上一篇: 我应该使用静态的
下一篇: 强制转换以在编译时检查继承