How do I use heterogeneous lookup with std::list?

In C++, heterogeneous lookup refers to the ability of the Standard Template Library (STL) to find elements in different types of containers. The `std::list` is a sequence container that allows for fast insertion and deletion of elements from any position in the container. This makes it versatile for various use cases. Below is an example of how you can use `std::list` to store different types of entries.

#include #include #include class Base { public: virtual void print() const { std::cout << "Base class" << std::endl; } virtual ~Base() {} }; class DerivedA : public Base { public: void print() const override { std::cout << "DerivedA class" << std::endl; } }; class DerivedB : public Base { public: void print() const override { std::cout << "DerivedB class" << std::endl; } }; int main() { std::list myList; myList.push_back(new DerivedA()); myList.push_back(new DerivedB()); for (const auto& item : myList) { item->print(); } // Clean up memory for (const auto& item : myList) { delete item; } return 0; }

C++ std::list heterogeneous lookup sequence container STL