Martin R.
Martin R.

Reputation: 67

Using reinterpret_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

Upvotes: 0

Views: 3458

Answers (1)

BlamKiwi
BlamKiwi

Reputation: 2193

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

Upvotes: 1

Related Questions