Boost logo

Boost :

Subject: Re: [boost] Re : BOOST_FOREACH like macro for map
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2009-10-21 09:03:35

mobi phil wrote:
> 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

How does a macro reduce the code you write?

Your example is:

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

My version was:

   data_type & data(get_value(aStringMap, "key"));

Looking at your example here, it seems that you want this logic:

   if (find "key" in aStringMap)
      // initialize data to reference the value for "key"

I had thought you wanted to assume that the value was always found.

Given what I now understand, your macro would need to be something like this (ignoring necessary encapsulation details and not implementing the std::map to type mappings):

#define BOOST_CONSTMAPFIND(_map, _key, _value) \
   const_iterator_type_from(_map) const it(_map.find(_key));
   typedef data_type_from(_map) data_type;
   data_type dummy;
   data_type * value(&dummy);
   data_type & _value(*value);
   if (it != _map.end())
      value = &it->second;

> furthermore:
> 1. exception... throwing the exception removes the complexity of
> writing but introduces the complexity of exception (writing code +
> runtime).

I had understood your initial use case to imply that you assumed the value was found. I didn't recognize that you meant the macro to be a conditional controlling the following block.

> 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

Not all types have an unused value to indicate invalidity.

> 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

That's the unimplemented part in my macro above.

> 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)
> {
> processResult(data);
> }

You need the iterator type, too.

Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

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