|
Boost : |
From: Jeremy Siek (jsiek_at_[hidden])
Date: 2000-12-07 11:41:32
There's several problems:
- if the compiler doesn't respect for-loop scoping,
then if "i" is also defined in the enclosing scope
there will be a problem, and because of the macro,
someone may not realize that "int i" is being defined.
- using "int" is typically a bad idea. The "n" is
probably from the size() of some container, and therefore
should have type "size_type". Comparing "int" with
"size_type" in "i < n" will give warnings about comparing
signed and unsigned integers. So "i" ought
to be the "size_type" for the container.
- the main purpose of code is communication... not
between you and the machine, but between you and other
programmers. Such macros obscure the meaning of
the code.
On Thu, 7 Dec 2000 rwgk_at_[hidden] wrote:
> On request of David Abrahams I removed my beloved iterator
> macros from a few 'getting started' examples that I wrote
> for the Boost Python Library.
>
> Here is what I normally use:
>
> #ifndef DEFINE_RANGE_H
> #define DEFINE_RANGE_H
>
> #define rangei(n) for (int i = 0; i < n; i++)
> #define range1(i, n) for (int i = 0; i < n; i++)
> #define range2(i, f, n) for (int i = f; i < n; i++)
> #define range3(i, f, n, s) for (int i = f; i < n; i += s)
>
> #endif // DEFINE_RANGE_H
>
> This simple include file allows me to make my source code
> look a lot more concise. What is bad with this approach?
> Is it just "bad style" (why?), or are there more fundamental
> (i.e. real) problems?
>
> This is probably not the most important question in the world,
> but each time I see myself typing (for int i = 0; i < 3; i++)
> I keep saying to myself that there must be a better way of
> doing this...
>
> Ralf
>
>
>
>
>
>
----------------------------------------------------------------------
Jeremy Siek www: http://www.lsc.nd.edu/~jsiek/
Ph.D. Candidate email: jsiek_at_[hidden]
Univ. of Notre Dame work phone: (219) 631-3906
----------------------------------------------------------------------
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk