At Meeting C++ 2019, Jon Kalb gave a talk about template techniques and mentioned policy classes. See here for the source: https://youtu.be/MLV4IVc4SwI?t=1815
The interesting code snippet in question is:
template<class T, class CheckingPolicy>
struct MyContainer : private CheckingPolicy
{
...
}
I've seen this type of design quite often and I was wondering if inheritance here has any real advantages over composition. In my personal experience I've heard much about the Prefer composition over inheritance paradigm. So the way I would have written the code would be more like this:
template<class T, class CheckingPolicy>
struct MyContainer
{
CheckingPolicy policy;
...
}
There wouldn't be any virtual functions involved. Nevertheless I'd appreciate it if you could share some insights how these differ. I would be especially interested in differences in memory layout and its implications. Would it make a difference if CheckingPolicy
has no data members, but only a check
method or an overloaded call operator?