As for the compiler, std::list<A> and std::list<B> are disjoint types. This makes sense when you consider that std::list has internal allocated memory for objects A Trying to assign objects B to this space can be catastrophic.
Imagine, for example, that B has an additional property. Now, if you are trying to keep B in memory large enough for A , this is most likely not suitable. You can use the same logic to understand why the other direction also fails: if you store A in the space for B , the compiler expects that the additional property B , which in his opinion is in this space, is valid, If you assigned A , although in this space there is someone who knows what is in it.
If you want this task to work, you will need to use some form of indirection. For example, you can use two instances of std::list<A*> or two instances of std::list<std::shared_ptr<A>> . This then works because B* can be safely regarded as A* , at least assuming the classes are correctly written.
Corbin
source share