How to do it...

Let's implement a program that knows the types, cat and dog, and that stores a mixed list of cats and dogs without using any runtime polymorphy:

  1. First, we include all the needed headers and define that we use the std namespace:
      #include <iostream>      #include <variant>      #include <list>      #include <string>      #include <algorithm>            using namespace std;
  1. Next, we implement two classes that have similar functionality but are not related to each other in any other way, in contrast to classes that, for example, inherit from the same interface or a similar interface. The first class is cat. A cat object has a name and can say meow:
      class cat {          string name;      public:          cat(string n) : name{n} {}
                void meow() const { cout << name << " ...

Get C++17 STL Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.