I want to check if the type given to a template is inherited from a base class in my project.
It should work like one would expect it from the following example:
template< class T : public CBaseClass >
It is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.
Template classes and functions can make use of another kind of template parameter known as a non-type parameter. A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument.
The difference is, that the compiler does type checking before template expansion. The idea is simple, source code contains only function/class, but compiled code may contain multiple copies of the same function/class. Function Templates We write a generic function that can be used for different data types.
A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)
Shorter is better:
template <typename Base, typename Derived>
struct is_base {
constexpr static bool check(Base*) { return true; }
constexpr static bool check(...) { return false; }
enum { value = check(static_cast<Derived*>(0)) };
};
Example 1:
struct A {};
struct B : A { };
int main(void) {
static_assert(is_base<A,B>::value, "If Error: A is not base of B");
}
Example 2:
template <bool, typename T=void>
struct Use {
static std::string info() { return "Implementation that consider that A is not base of B"; }
};
template <typename T>
struct Use<true,T> {
static std::string info() { return "Implementation that consider that A is the base of B"; }
};
int main(void) {
std::cout << Use<is_base<A,B>::value>::info(); //Implementation that consider that A is the base of B
}
Following an example from Stroustrup:
template<class Test, class Base>
struct AssertSameOrDerivedFrom {
AssertSameOrDerivedFrom() { &constraints; }
public:
static void constraints() {
Test *pd = 0;
Base *pb = pd;
}
};
template<class T>
struct YourClass {
YourClass() {
AssertSameOrDerivedFrom<T, CBaseClass>();
}
};
In C++0x, this becomes:
template<class T>
struct YourClass {
static_assert(std::is_base_of<CBaseClass, T>::value);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With