I have a simple struct
struct A {
int t;
int s;
};
Those were contained in std::list<A>
. The problem is that I group A depending on s. I can avoid duplication of s if I'd wrap A in another struct and keep s in a separated container. This has a lot of benefits (e.g. I don't need to do stupid and potentially dangerous things like AContainer[0].s
to get the value of s for the whole list). The wrapper would look like this:
struct AList {
std::list<A> list;
int s;
}
The problem is that I'd still like my AList to behave as if it were a std::list<A>
. This is simple if I replicate the whole interface of std::list
but has a few drawbacks: changing the container would involve inspecting all functions that expose the interface, and the huge amount of duplicated code. On the other hand: just using the list makes the code a lot less readable.
Are there any standard solutions or helper classes for such a case? Is inheriting from std::list a good idea? I have never seen it before and most people argue that STL containers don't have virtual dtors. This wouldn't be a problem for me in this situation but is probably relevant to the larger question here.