Note that these two samples are only functionally equivalent if A
instances are singletons. Otherwise it's very possible that two A
values which are equal in value but different in address. This would lead to different semantics.
Personally I prefer the std::map<A, std::string>
version because the semantics of it are crystal clear. The keys have equality semantics and there is no potentially for a dangling or nullptr
value. The std::map<A*, std::string>
version comes with a host of questions for the developer looking through the code
- Who owns the key values?
- Are all instances of
A
singletons? If not how do I ensure theA
I'm looking for is theA*
value that is stored? - When are the keys freed?