Boost logo

Boost :

From: A. Azuma (cryolite.indigo_at_[hidden])
Date: 2005-10-07 11:55:17


Hi, Boosters.

First, sorry for my bad English and the long post.

I have been much inspired by Eric Niebler's FOREACH. It is very nice and useful.
However, I think there are some drawbacks in his implementation. First, Eric's
FOREACH should explicitly specify the value type of the range. It is
still bothering
me in some cases. For example, std::map or a range of ranges. Second, the
reference type should be declared when a value is written into the
range. It seems
unclear.

Then, I show yet another idea to implement FOREACH. My implementation can
provide the following syntax.

vector<int> v;
.....
FOREACH(iter, v){
  cout << *iter << endl;
}

The code above can be considered that `iter' is declared as an iterator.
In this syntax, the first problem I picked up is clearly solved.
Moreover, when writing a value into the range, the code whould be

int i = 0;
FOREACH(iter, v){
  *iter = i++;
}

It seems very clear. The second problem is solved, too. This syntax would be
reasonable and familiar to C++ programmers. Of cource, all the stuffs
in the macro
and operator* are calculated in compile-time, and compilers can optimize at the
same level as an equivalent handwritten for-loop.

On the other hand, there is a very serious limitation in my code. A number of
FOREACH in each translation unit is limited. With the implementation in the
attached file, the number is limited up to 100. This number can be easily
increased, but increasing it burdens a compiler. In addition, member access
operator (operator->) cannot be used.

I use a subtle implementation of compile-time global counter to emulate
auto/decltype, and subtly hides them in FOREACH macro and iterator syntax.
Please see the attached code for more detail. The attached code has been tested
on GCC3.4.2, MSVC7.1 and MSVC8.0 beta 2. All the implementations are
compliant to the standard, I believe so at least.

By the way, my idea contains auto/decltype emulation, so such a syntax as
followings can be also provided by using my idea.

AUTO_ANY(less, boost::lambda::_1 < boost::lambda::_2);
sort(v1, v2, auto_cast(less)); // the type of the lambda functor is restored.

Of course, this does not look so nice.

I would like to ask boosters how useful, portable and interesting my idea is.

Thanks in advance.




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