Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2005-04-25 17:16:20


Stefan Strasser wrote:
> michael toksvig schrieb:
>>
>> BOOST_FOREACH(pair<Key, Value> &p, some_map)
>>
>> does not work, since the preprocessor just can't get over that first
>> comma
>>
>
> this could be worked around with C99 variadic macros if available,
> something like:
>
> #define _BOOST_FOREACH2(a1,a2,...) BOOST_FOREACH2(a1,a2)
> #define _BOOST_FOREACH3(a1,a2,a3,...) BOOST_FOREACH3(a1,a2,a3)
> // ...
>
> #define _BOOST_FOREACH(a1,a2,a3,a4,a5,a6,a7,a8,a9,nr,...)
> _BOOST_FOREACH##nr(a1,a2,a3,a4,a5,a6,a7,a8,a9)
>
> #define BOOST_FOREACH(...) _BOOST_FOREACH(__VA_ARGS__,9,8,7,6,5,4,3,2,1)
>

Clever, but there is an ambiguity here. What is the relations of the
arguments in BOOST_FOREACH2(a1,a2,a3)? Do a1 and a2 together make up the
loop variable declaration, or do a2 and a3 together form the collection
expression:

case 1:
   BOOST_FOREACH( std::pair<int,int> p, some_map )

case 2:
   BOOST_FOREACH( int i, some_trait<this,that>::some_list() )

I suppose you could say that case 2 can always be handled with a set of
parens, as in:

   BOOST_FOREACH( int i, (some_trait<this,that>::some_list()) )

Therefore, all macro args but the last should be interpreted as part of
the loop variable declaration.

Anyway, I haven't heard of a major C++ compiler supporting C99 varargs.
Is there one? I don't think we have a config macro for detecting it.

Michael Toksvig's original point is a good one though. There should be a
pitfalls section in the doc that warns people of this. I'll put it on my
list of ToDo's.

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk