As a side note, if we were to implement the previous example with traditional runtime polymorphism, using inheritance and virtual functions to achieve the same functionality, the implementation would look like the code example below:
struct AnimalBase { virtual ~AnimalBase() {} virtual auto speak() const -> void {} }; struct Bear : public AnimalBase { auto roar() const { std::cout << "roar"; } auto speak() const override -> void { roar(); } }; struct Duck : public AnimalBase { auto quack() const { std::cout << "quack"; } auto speak() const override -> void { quack(); } }; auto speak(const AnimalBase* a) { a->speak(); }
The objects have to be accessed using pointers or references, and the type is ...