Instead of writing town->first
I would like to write town->name
. Inline named accessors (Renaming first and second of a map iterator and Named std::pair members) are the best solutions I have found so far. My problem with named accessors is the loss of type safety:
pair<int,double>
may refer to struct { int index; double value; }
or to struct { int population; double avg_temp; }
. Can anyone propose a simple approach, perhaps something similar to traits?
I often want to return a pair or a tuple from a function and it is quite tiring to introduce a new type like struct city { string name; int zipcode; }
and its ctor every time. I am thrilled to learn about boost and C++0x but I need a pure C++03 solution without boost.
Update
Re andrewdski's question: yes, a (hypothetical) syntax like pair<int=index, double=value>
which would create a distinct type from pair<int=population, double=avg_temp>
would meet your requirement. I do not even mind having to implement a custom pair/tuple template class ONCE and just passing a 'name traits' template argument to it approprietly when I need a new type. I have no idea how that 'name traits' would look like. Maybe it's impossible.