c++ check that templates parameters are derived from a certain base class - c++

How can I check whether my template parameters are derived from a certain base class? So that I am sure that the function Do can be called:
template<typename Ty1> class MyClass
{
...
void MyFunction();
};
template<typename Ty1> void MyClass<Ty1>::MyFunction()
{
Ty1 var;
var.Do();
}

Don't. If the method Do() doesn't exist in the class provided as an argument for Ty1, it will simply not compile.
Templates are a form of duck typing : the abilities of a class are not determined by what interface it inherits from, but by what functionality it actually exposes.
The advantage is that your template can then be used by any class with a suitable Do() method, regardless of where it came from or what bases it has.

You can achieve this using standard type trait is_base_of. Look at the example:
#include <iostream>
#include <type_traits>
using namespace std;
class Base {
public:
void foo () {}
};
class A : public Base {};
class B : public Base {};
class C {};
void exec (false_type) {
cout << "your type is not derived from Base" << endl;
}
void exec (true_type) {
cout << "your type is derived from Base" << endl;
}
template <typename T>
void verify () {
exec (typename is_base_of<Base, T>::type {});
}
int main (int argc, char** argv) {
verify<A> ();
verify<B> ();
verify<C> ();
return 0;
}
And the output is:
your type is derived from Base
your type is derived from Base
your type is not derived from Base

Related

c++ 20 concepts in derived template class

for my test about CRTP I created this base class:
template<typename Derived>
struct Base
{
void print() const { std::cout << "print\n"; }
};
And this class:
struct A: public Base<A>
{
void printSub() { std::cout << "printSub from A\n"; }
};
And I can use these classes without problem:
A a1{};
a1.print();
a1.printSub();
My problem is: how is possible to use concept/requires for template class?
I want add something like this:
template<typename Derived> requires requires (Derived t) {
t.printSub();
}
struct Base
{
void print() { std::cout << "print\n"; }
};
To accept only Derived class with printSub method. But I receive this error:
template constraint failure...
I did some tests but I did not find solutions (I also found this thread but it can't help me).
Where am I wrong? Thanks for any help
You cannot meaningfully constrain the derived class template parameters of a CRTP base class for the same reason that you can't do this:
template<typename Derived>
class Base
{
using alias = Derived::SomeAlias;
};
Derived isn't complete yet, and using a constraint usually requires completeness.

Use base class implementation when base is template type

I have a class that receives its base type as a template arg and I want my derived class to call a function, print. This function should use the derived implementation by default but if the base class has a print function it should use the base implementation.
#include <iostream>
class BaseWithPrint {
public:
static void print(int i) { std::cout << "Base::print\n"; }
};
class BaseWithoutPrint {
};
template <typename B>
class Derived : public B {
public:
static void print(bool b) { std::cout << "Derived::bool_print\n"; }
template <typename T>
static void print(T t) { std::cout << "Derived::print\n"; }
void Foo() {
print(1);
print(true);
print("foo");
}
};
int main()
{
Derived<BaseWithPrint> d1;
d1.Foo();
Derived<BaseWithoutPrint> d2;
d2.Foo();
return 0;
}
This code only ever calls the Derived version of print.
Code can be seen at
https://onlinegdb.com/N2IKgp0FY
If you know that the base class will have some kind of print, then you can add using B::print to your derived class. If a perfect match isn't found in the derived, then it'll check the base.
Demo
To handle it for the case where there may be a base print, I think you need to resort to SFINAE. The best SFINAE approach is really going to depend on your real world situation. Here's how I solved your example problem:
template <class T, class = void>
struct if_no_print_add_an_unusable_one : T {
// only ever called if derived calls with no args and neither
// the derived class nor the parent classes had that print.
// ie. Maybe best to force a compile fail:
void print();
};
template <class T>
struct if_no_print_add_an_unusable_one <T, decltype(T().print(int()))> : T {};
//====================================================================
template <class B>
class Derived : public if_no_print_add_an_unusable_one<B> {
using Parent = if_no_print_add_an_unusable_one<B>;
using Parent::print;
public:
// ... same as before
};
Demo

overriding the template base method in derived class?

Suppose I have a base class as below:
template <typename T>
class Base {
// implementation
void do_something() { /* ... */ } ;
};
then, I create a Derived class as below, and override the do_something() method:
template <typename T>
class Derived : public Base<T> {
// implementation
void do_something() { /* ... */ } ;
};
I know virtualization does not work in class templates, and I am just hiding the implementation of the methods. but I do want to store a bunch of derived classes and base classes into a vector, (I do not want to use type erasure, or polymorphism),
my question is, given that static_cast of Derived class to base class gives me the do_something of based class, Is there any way that I can store them as base classes while each has their implementation of do_something() class ?
but I do want to store a bunch of derived classes and base classes into a vector, (I do not want to use type erasure, or polymorphism),
This is already just not possible in C++. In C++, a vector can only contain objects of the same static type. The only way a vector can contain different types of objects is if their static type is still the same, but they have different dynamic types, but this is type erasure/polymorphism which you said you don't want to use.
I think maybe you need to rethink your requirements, because your question in essence reads: I want to do something, but I don't want to use technique X which is explicitly defined as the only way to do that something in C++!
I did this and it seems to work fine:
#include <iostream>
template <typename T>
struct Base {
virtual void do_something() { std::cout << "Base::do_something()\n"; }
};
template <typename T>
struct Derived : public Base<T> {
virtual void do_something() { std::cout << "Derived::do_something()\n"; }
};
int main() {
Base<int> b;
Derived<int> d;
Base<int> *p;
p = &b;
p->do_something();
p = &d;
p->do_something();
return 0;
}
Output:
Base::do_something()
Derived::do_something()
A little variation of the melpomene's answer (adding a no-template base struct, BaseOfBase, for the Base<T> structs) permit the use of a common vector of base of derived classe of different T types.
A working example
#include <vector>
#include <iostream>
struct BaseOfBase
{ virtual void do_something () = 0; };
template <typename T>
struct Base : public BaseOfBase
{
T val;
void do_something ()
{ std::cout << "Base::do_something() [" << val << "]\n"; };
};
template <typename T>
struct Derived : public Base<T>
{ void do_something()
{ std::cout << "Derived::do_something() [" << this->val << "]\n"; } };
int main ()
{
std::vector<BaseOfBase*> vpbb;
Base<int> bi;
Derived<int> di;
Base<std::string> bs;
Derived<std::string> ds;
bi.val = 1;
di.val = 2;
bs.val = "foo";
ds.val = "bar";
vpbb.push_back(&bi);
vpbb.push_back(&di);
vpbb.push_back(&bs);
vpbb.push_back(&ds);
for ( auto const & pbb : vpbb )
pbb->do_something();
}
When we say virtualization doesn't work in template classes, we don't mean that you can't do virtual functions in a template class, nor does it mean that you cannot override a member function with a specialized version of it.
#melpomene showed an example of overriding in general, and I will show here with specialization:
#include <iostream>
template <typename T>
class Base {
public:
virtual T do_something(T in) { std::cout << "Base::do_something()\n"; return in; }
};
class Derived : public Base<int> {
public:
virtual int do_something(int in) { std::cout << "Derived::do_something()\n"; return in - 1; }
};
void main()
{
Base<int> b;
Derived d;
Base<int> *p = &b;
auto r1 = p->do_something(10);
std::cout << r1 <<std::endl;
p = &d;
auto r2 = p->do_something(10);
std::cout << r2 << std::endl;
}
Which will output
Base::do_something()
10
Derived::do_something()
9
Showing that it perfectly works as expected.
What we do mean when saying that
virtualization does not work in class templates
Basically means that you can't use as a template the derived class when the base is expected.
Consider the above classes Base<T> and Derived, then if we have the following code:
#include <memory>
template <typename T>
void Test(std::unique_ptr<Base<T>> in){ std::cout << "This will not work with derived"; }
void main()
{
Base<int> b;
Derived d;
auto ptr = std::unique_ptr<Derived>(&d);
Test(ptr); // <-- Will fail to compile as an invalid argument
}
it will fail because std::unique_ptr<Derived> does not inherit from std::unique_ptr<Base<T>> although Derived itself inherits from Base<T>.

C++ calling a inherited method of a templated superclass

I don't understand why the following code does not compile:
#include <iostream>
using namespace std;
template<typename T>
class Base {
public:
void printTuple(T tuple) {
std::cout << "Hello1!" << std::endl;
}
};
template<typename T>
class Derived : public Base<T> {
public:
void printTuple(std::string name, T tuple) {
std::cout << "Hello2!" << std::endl;
printTuple(tuple);
}
};
int main()
{
Derived<int> d1;
d1.printTuple("Test", 13);
return 0;
}
I get the following error:
main.cpp:19:25: error: no matching function for call to Derived::printTuple(int&)'
But shouldn't Derived inherit a method with such a signature from Base?
Thanks
You should change the line printTuple(tuple) to Base<T>::printTuple(tuple) because a function of the base class has been hidden.
Just add this to the top of the public part of class Derived:
using Base<T>::printTuple;
This will expose the base class overload of the function, i.e. prevent it from being "shadowed."

How do you overload templated function (specialized) in child class?

I have a base class with a templated function that has the general templated type, as well as specialized version.
#ifndef BASE_CLASS
#define BASE_CLASS
#include <iostream>
using namespace std;
struct Type1
{
};
struct Type2
{
};
class baseClass
{
public:
template<class Type>
void doStuff(Type & t)
{
templateFunction(t);
}
template<class Type>
void templateFunction(Type & t);
};
template<class Type>
void baseClass::templateFunction(Type & t)
{
cout << "This is the generic function!" << endl;
}
template<>
void baseClass::templateFunction(Type1 & t)
{
cout << "This is the specialized function: - Type1" << endl;
}
#endif
I also have a child class, that inherits from "baseClass". However, the child class requires different functionality for that specialization.
#ifndef CHILD_CLASS
#define CHILD_CLASS
#include "BaseClass.h"
class ChildClass : public baseClass
{
public:
};
template<>
void ChildClass::templateFunction(Type1 & t)
{
cout << "We overloaded the specialized template function for type 1!" << endl;
}
#endif
The above does not compile:
ChildClass.h:13: error: no member function âtemplateFunctionâ declared in âChildClassâ
ChildClass.h:13: error: invalid function declaration
If I change the "overloaded" function to:
template<>
void baseClass::templateFunction(Type1 & t)
{
cout << "We overloaded the specialized template function for type 1!" << endl;
}
I get:
ChildClass.h:13: error: redefinition of âvoid baseClass::templateFunction(Type&) [with Type = Type1]â
BaseClass.h:36: error: âvoid baseClass::templateFunction(Type&) [with Type = Type1]â previously declared here
How do I properly overload specialized template functions in child classes?
For reference, the main:
#include "BaseClass.h"
#include "ChildClass.h"
int main()
{
Type1 first;
Type2 second;
baseClass theBaseClass;
ChildClass theChildClass;
theBaseClass.doStuff(first);
theBaseClass.doStuff(second);
theChildClass.doStuff(first);
theChildClass.doStuff(second);
return 0;
}
On the suggestion of: Kerrek SB, I've changed the ChildClass to:
#ifndef CHILD_CLASS
#define CHILD_CLASS
#include "BaseClass.h"
class ChildClass : public baseClass
{
public:
template<class Type>
void templateFunction(Type & t);
};
template<>
void ChildClass::templateFunction(Type1 & t)
{
cout << "We overloaded the specialized template function for type 1!" << endl;
}
#endif
The output:
This is the specialized function: - Type1
This is the generic function!
This is the specialized function: - Type1
This is the generic function!
I was hoping for:
This is the specialized function: - Type1
This is the generic function!
We overloaded the specialized template function for type 1!
This is the generic function!
So this still doesn't work.
The reason why it still doesn't work the way you wanted to is that the function is not virtual in the parent class. However, it is not possible to have a virtual template function.
I see two main options:
as rhalbersma suggested, make the class itself template and then override the desired method s (which now are NOT template) in the child class.
for the specialized method, just write a new method, with a different name, that does whatever you need.
But I'm sure someone will come up with a better idea... =)
You could make a template class with a virtual function like this:
template<typename T>
class B
{
virtual void f() { std::cout << "base template implementation \n"; }
};
template<>
class B<Type1>
{
virtual void f() { std::cout << "base specialized implementation \n"; }
};
template<typename T>
class D: public B<T>
{
virtual void f() { std::cout << "derived template implementation \n"; }
};
template<>
class D<Type1>: public B<Type1>
{
virtual void f() { std::cout << "derived specialized implementation \n"; }
};
B<Type1> b1;
B<Type2> b2;
D<Type1> d1;
D<Type2> d2;
b1.f();
b2.f();
d1.f();
d2.f();
Now there are 2 independent dimensions of customization: either the template T, or the dynamic type (B vs D). For each template instantiation, the virtual functions can be redefined.
More common is to let B be a regular class with pure virtual functions (i.e. an interface), and let D be a template class deriving from B. This lets you redefine the virtual functions of B in a different way for each template instantiation of D (with a suitable default).