Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2005-10-12 11:39:16

A. Azuma wrote:
> 2005/10/9, Daniel James <daniel_at_[hidden]>:
>>Unfortunately, this technique is not standards compliant. The problem is
>>in this code:
> Ah, I see. I overlooked this problem. I'm ashamed of my
> misinterpretation . After a
> little thought, I have concluded that there might not be any tricks to
> enable this
> kind of ADL. I would have to abandon my idea. Does anyone have good idea?

No reason to be ashamed -- it was a clever idea. There was one other
person during the FOREACH review who argued for a similar interface,
using TYPEOF in the implementation. There are a few reasons IMO why it
wasn't a good idea:

1) It would have required people to register types with TYPEOF in order
to use FOREACH. Unacceptable, IMO. (Your idea doesn't suffer from this,
but it's non-standard.)

2) The current signature of FOREACH (FOREACH(type var, sequence))
mirrors the foreach construct of other popular languages. Also, this is
the signature likely to be adopted should a new foreach looping
construct be added to C++.

3) FOREACH(iter, sequence) doesn't make as strong a guarantee as does
FOREACH(type var, sequence). With the second, you can be sure that every
element in the sequence wiil be visited exactly once, and we won't walk
off the end of the sequence. But with FOREACH(iter, sequence), nothing
is stopping people from burying a ++iter somewhere in the loop body,
throwing off the looping mechanism. When you need an iterator, there is
always the general for(;;) statement. Use FOREACH for the simple, common

Eric Niebler
Boost Consulting

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