Re: fwd declaring STL containers
Victor Bazarov wrote:
Mark P wrote:
LR wrote:
[..]
Now I'm curious. How does this simplify client syntax?
Compare the following two declarations:
std::map<Key, Ty, std::less<Key>,
myAlloc<std::pair<const Key,Ty> > > myMap;
my_map<Key,Ty>::Type myMap;
The issue is that the allocator parameter is the last among all
parameters so to override the default it's necessary to specify all
parameters. Compound this with the particularly unwieldy value_type
of the map, and it gets pretty ugly.
Yes, when you want to supply some kind of pre-defined umpteenth template
argument, then a typedef is an easy way out. But how often do you have
to do that, really?
Short answer: everywhere. I probably have hundreds of such declarations
spread over dozens of source files. Every instance of an STL container
has to use this allocator and individual typedefs help a little, but
there are still a lot of them.
I try hard to keep my code within an 80 column width and these map
declarations are often spread over 3 lines in order to adhere to this
(since my template parameters are rarely so compact as "Key" and "Ty"
above). I'll concede to being a bit anal when it comes to formatting
code but to me this reduced syntax really helps the readability (and
rest assured I choose a slightly more informative name than "my_map" to
indicate the default custom allocator).
I guess that along with a custom allocator you could
define your own typedefs for using the same allocator with the standard
containers... But that would be also hiding the fact that a custom
allocator is used... I just prefer everything explicit.
I don't think I understand your suggestion here, unless you're restating
the "template typedef" notion I mentioned in my original post?
Thanks,
Mark