Boost logo

Boost :

Subject: Re: [boost] Re : BOOST_FOREACH like macro for map
From: mobi phil (mobi_at_[hidden])
Date: 2009-10-20 18:42:19

Thanks for the replies... However

I forgot to put in my question that I am looking for a macro and not
template if possible, and for a solution as elegant as BOOST_FOREACH,
where you have to code as less as possible. It not (only :) ) that I
am lazy to write, but I think the resulting code would be easier to

1. exception... throwing the exception removes the complexity of
writing but introduces the complexity of exception (writing code +

2. pointer... use cases I would like to avoid pointers, as I have
other constraints to work with const references.

I was thinking all the day about this little "handicap" of the
std::map. One way would be to have the concept of invalid object of
the map data type that could be returned to satisfy the constraint of
the reference, and a "horrible" function like

bool return;
const data_type & data = map.find(key, bool)
if( return ) {
   //use the data
With this approach is that the "return" variable has to be declared.

with the form I proposed

BOOST_CONSTMAPFIND(aStringMap, "key", data)
     processResult( data );

the problem is that I do not know how to detect the type of data
knowing in the map object. I thought for the moment that BOOST_FOREACH
would "automatically" declare the type in the container (using
non-standard typeof for eg), but revisiting again BOOST_FOREACH
documentation i see that it declares the type.
vector<int> v;
BOOST_FOREACH( int i, v) etc.

so it seems that if I declare the type of the data, my job becomes
easy, a sort of "problem solved"

const map<string, Mytype> & mymap = getMap();
//I changed the order of variables
BOOST_CONSTMAPFIND(const Mytype &data, "key", mymap)
and pair for non-const
BOOST_MAPFIND(Mytype &data, "key", mymap)

probably with some trick (probably sthg. similar used in
BOOST_FOREACH), one could merge theh two macros.

I am now two tired, but will write tomorrow the macro, test it, and
would be happy if boost would accept it into the macros family.

mobi phil
being mobile, but including technology

Boost list run by bdawes at, gregod at, cpdaniel at, john at