How can I ensure that the template parameter used implements some interface in C ++?

I don't think this is possible in C ++, what parameters do I need to simulate behavior?

+4
source share
3 answers

You can use std::is_base_of<YourInterface, YourParameter> and make a mistake if the result is false . Remember this is C ++ 11.

+6
source

Use std::is_base_of as:

 template<typename T> class A { static_assert(std::is_base_of<IMyInterface, T>::value, "T must derive from IMyInterface"); }; 

You can also use a function template.

+9
source

Some polymorphic interfaces or some static interface? The latter can be checked using the "Concept of increasing . "

+3
source

All Articles