"Inheritance is the base class of evil" - Sean Parent
Roger, copy that! But sometimes I really want my classes to "import" data members or functions which are better organized elsewhere. And I can do a lot with single inheritance, multiple inheritance, variadic inheritance. Awesome. But when I want to be decent and use composition?
- I have to know how many members I want to add in advance. There is no variadic composition.
- I have to decide on the names of those members in advance. There is no compile time way of choosing a name.
- And if I want my class to offer methods of those members, I have to write forwarding functions and need to know the name of those methods in advance. If the interface of the member changes, my forwarding functions are likely to be broken.
Composition is quite limited when compared to inheritance. That's why we use inheritance so much, especially with templates.
I'll give a few examples, including a real-life case of variadic CRTP and an inheritance-based way of how to specify the names of your data members via template parameters. After that, I'll open the discussion by suggesting two conceptually simple language features and how they could improve our life:
- Mixin classes
- Name providers (still looking for a better name).