Suppose we have the following template class
template<typename T> class Wrap { /* ... */ };
We can not change Wrap. It is important.
Let there are classes derived from Wrap<T>. For example,
class NewInt : public Wrap<int> { /* ... */ };
class MyClass : public Wrap<myclass> { /* ... */ };
class Foo : public Wrap<Bar> { /* ... */ };
We can not change these classes too. All classes above is 3rd party. They are not mine.
I need the following compile time type_traits:
template<class T>
struct is_derived_from_Wrap {
static const bool value = /* */;
};
What do I need?
assert(is_derived_from_Wrap<Int>::value == true); // Indeed I need static assert
assert(is_derived_from_Wrap<MyClass>::value == true);
assert(is_derived_from_Wrap<char>::value == false);
struct X {};
assert(is_derived_from_Wrap<X>::value == false);
You can do this using SFINAE but its kind of magical if you dont know whats going on…
This gives the expected output
There are a couple of gotchas with my code. It will also return true if the type is a Wrap.
This can probably be fixed using a bit more SFINAE magic if needed.
It will return false if the derivation is not a public derivation (i.e is private or protected )
I suspect this can’t be fixed. (But I may be wrong ). But I suspect public inheritance is enough.