For a templated class, I would like to be able to require that the template typename extends a certain class. The reason for this is to ensure that the correct methods are implemented.
I have written a mock example, in this case the class "tester" expects its template typename to have the method "test" associated with it (this is achieved by inheritance of supertype). How could I prevent a future developer/user trying to pass a class template which does not extend the supertype class?
class supertype{
public:
virtualint test()=0;
};
class subType1 : public supertype{
public:
virtualint test(){return 1;};
};
class subType2 : public supertype{
public:
virtualint test(){return 2;};
};
template < class T >
class tester{
public :
int run(){
T obj;
return obj.test();
}
};
tester<subType2> a1
a2.run();
tester<int> a2;
//I want this to not compile.
a2.run();
(This code is just to illustrate my question, so it might not compile.)
It appears to me that you are attempting to solve too many problems at once.
What you have posted basically does exactly as you are expecting. After a few syntactical updates, it compiles and line 30 does not compile because int is not a user-defined type with a method signature 'int test()' accessible by tester. The compiler knows that int is not a suitable type for tester and it would also know if you created a class that did not have the same test method signature.
The problem (I'm assuming this is your concern) is that if you created an arbitrary class that coincidentally defines a public method with that signature, it would compile. Even though it is not a subclass of your superclass.
This question remains, though: why are you trying to use templates? You could overcome the limitations that I have mentioned by passing a reference or pointer to the constructor of the tester class. tester could store the reference/pointer as a private member and invoke the appropriate method on it inside of its run method. This does, however, mean that the user of the tester class now has the responsibility of instantiating the superclass object before creating the tester object. If you have additional concerns about their construction, that would be a separate problem that needs to be solved.
Thank you very much for all your replies, its really helpful.
PanGalactic, yes it will compile but as moorecm says;
The problem (I'm assuming this is your concern) is that if you created an arbitrary class that coincidentally defines a public method with that signature, it would compile. Even though it is not a subclass of your superclass.
moorecm, you're right, I don't really need templates in this case, it just complicates it unnecessarily. Your solution is much neater and works. :)
blackcoder41, in case your interested in the result, when I tried
1 2 3
template < supertype T >
class tester{
public :
I got the error;
error: 'class supertype' is not a valid type for a template constant parameter
Also I am predominantly a java programmer, I clearly need to learn the C++ lingo.
And thanks for the info on boost and concepts, they have made an interesting read and will bare them in mind next time.
@cl41re
hehe, i expected that error. yeah why would you use template anyway. .
here a trivia, in c++ they call super class as base class, and derive class for sub class