Topic: for loop local variables


Author: David R Tribble <david.tribble@noSPAM.central.beasys.com>
Date: 1998/11/30
Raw View
Christopher Eltschka wrote:
> Would the following be legal, too?
>
> for(struct { int i; char c; } vars = { 0, 'x' }; vars.i<10; ++vars.i)
>   cout << vars.i << "-" << vars.c << endl;

HP-UX 10.20 C++ (not aC++) compiles this after giving it the '+a1'
option to allow automatic aggregate initializers.

> Maybe even this:
>
> for(struct { int i; char c; } = { 0, 'x' }; i<10; ++i)
>   cout << i << "-" << c << endl;

HP-UX complains that 'i' and 'c' are undefined.  Anonymous structs
probably don't work like anonymous unions.

-- David R. Tribble, dtribble@technologist.com --


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Christopher M. Gurnee" <gurnec_at_mediaone_dot_net@127.0.0.1>
Date: 1998/11/25
Raw View
Juan Carlos Gil Montoro wrote in message <365A8A06.B6F5F516@gmv.es>...
<clip>
>Now I'm wondering whether the standard allows
>to define two (or more) variables local to a for
>loop but *of different types*, in the line of:
>
>for(char c = 'x', int i = 0 ; i < 10 ; i ++)
>   cout << i << "-" << c << endl ;
>
>Is there a way of doing that ?

Here's an idea, although I'm not sure that I like it....

for (std::pair<char, int> c_i('x', 0); c_i.second < 10; ++c_i.second)
   std::cout << c_i.second << '-' << c_i.first << endl;


Here's an absolutely horrible idea :-)

// pair with a bunch of implicit conversions/assignments
template<typename T1, typename T2>
struct auto_pair : std::pair<T1, T2>
{
   auto_pair() {}
   auto_pair(const T1& t1, const T2& t2) : std::pair(t1, t2) {}
   template<typename U1, typename U2>
      auto_pair(const std::pair<U1, U2>& p) : std::pair(p) {}

   T1& operator=(T1& t)             {return first = t;}
   const T1& operator=(const T1& t) {return first = t;}
   operator T1&()                   {return first;}
   operator const T1&() const       {return first;}
   const T2& operator=(const T2& t) {return second = t;}
   T2& operator=(T2& t)             {return second = t;}
   operator T2&()                   {return second;}
   operator const T2&() const       {return second;}
}

for (auto_pair<char, int> c_i('x', 0); c_i < 10; ++c_i.second)
   std::cout << char(c_i) << '-' << int(c_i) << endl;

Imagine seeing something like this in production code... scary.  BTW, is
the "c_i < 10" in the for's condition legal?  My compilers complains of
ambiguity, but I think that it's OK (choose int over char).

-Chris Gurnee





[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1998/11/25
Raw View
On 25 Nov 1998 16:49:38 GMT, Christopher Eltschka

>Would the following be legal, too?
>
>for(struct { int i; char c; } vars = { 0, 'x' }; vars.i<10; ++vars.i)
>  cout << vars.i << "-" << vars.c << endl;

My editor doesn't like this: the '{' inside the parenthesis level
throws the syntax highlighting off.  But both como and egcs with
--pedantic compile and run it.  The named struct method has the
advantage that you can supply a constructor and operator++() for
your class.


>for(struct { int i; char c; } = { 0, 'x' }; i<10; ++i)
>  cout << i << "-" << c << endl;

Neither como nor egcs like this.  I have no idea why.


"k.c", line 8: error: expected an identifier
     for(struct { int i; char c; } = { 0, 'x' }; i<10; ++i)
                                   ^

"k.c", line 8: error: identifier "i"a is undefined
     for(struct { int i; char c; } = { 0, 'x' }; i<10; ++i)
                                                 ^

"k.c", line 9: error: identifier "c" is undefined
            cout << i << "-" << c << endl;
                                ^


k.c:8: parse error before `='
k.c:8: `i' undeclared (first use this function)
k.c:8: (Each undeclared identifier is reported only once
k.c:8: for each function it appears in.)
k.c:8: confused by earlier errors, bailing out



--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Christopher M. Gurnee" <gurnec_at_mediaone_dot_net@127.0.0.1>
Date: 1998/11/25
Raw View
Siemel Naran wrote in message ...
>On 25 Nov 1998 16:49:38 GMT, Christopher Eltschka
>
>>Would the following be legal, too?
>>
>>for(struct { int i; char c; } vars = { 0, 'x' }; vars.i<10; ++vars.i)
>>  cout << vars.i << "-" << vars.c << endl;
>
>My editor doesn't like this: the '{' inside the parenthesis level
>throws the syntax highlighting off.  But both como and egcs with
>--pedantic compile and run it.  The named struct method has the
>advantage that you can supply a constructor and operator++() for
>your class.

I thought of this, but rejected it after looking at the standard.  On
second look at the standard though, I can't seem to find anything that
disallows it (I misread the standard the first time through, big
surprise :-P ).  It explicitly is allowed by the syntax at least.

>>for(struct { int i; char c; } = { 0, 'x' }; i<10; ++i)
>>  cout << i << "-" << c << endl;
>
>Neither como nor egcs like this.  I have no idea why.

Unnamed structs are allowed in general, but they don't work like
anonymous unions; IOW, there are no anonymous structs or classes.  If
you get rid of the initializer, it won't compile because i and c don't
exist.

struct {int i; char c;};
union {int i; char c;};

The first line above has no affect.  The second (declared at block
scope) introduces the names i and c into that scope, and allocates space
for them as appropriate.

-Chris Gurnee



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Igor Rafienko <igorr@ifi.uio.no>
Date: 1998/11/24
Raw View
Juan Carlos Gil Montoro <jcgil@gmv.es> writes:

| The following is legal:
|
| for(int j = 1, i = 0 ; i < 10 ; i ++)
|    cout << i << "-" << j << endl ;
|
| Now I'm wondering whether the standard allows
| to define two (or more) variables local to a for
| loop but *of different types*, in the line of:
|
| for(char c = 'x', int i = 0 ; i < 10 ; i ++)
|    cout << i << "-" << c << endl ;
|
| Is there a way of doing that ?

You can try smth. similar (delimiting a var's scope to a loop):

void
foo()
{

   // ... some code
   {
     double x = 0.0;
     int count = 0;
     for ( ; count < 10; count++ )
       cout << x;
   }
   // ... more code
}

Try enclosing this for-loop in its own block. It's doubtfully as
elegant as declaring all vars in the initialization part, but I've
seen very clean code structured as the one above.



igorR
--
Vy vstaete s posteli, a lezhashaya ryadom devushka hvataet vas za ruku i
govorit:
- Save changes? Yes or No?


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: AllanW@my-dejanews.com
Date: 1998/11/24
Raw View
In article <365A8A06.B6F5F516@gmv.es>,
  Juan Carlos Gil Montoro <jcgil@gmv.es> wrote:
> The following is legal:
>
> for(int j = 1, i = 0 ; i < 10 ; i ++)
>    cout << i << "-" << j << endl ;
>
> Now I'm wondering whether the standard allows
> to define two (or more) variables local to a for
> loop but *of different types*, in the line of:
>
> for(char c = 'x', int i = 0 ; i < 10 ; i ++)
>    cout << i << "-" << c << endl ;

On a related note, I sometimes want to initialize one variable
while declaring another. Technically, this would work:
    int i;
    // ...
    // This for() initializes int i to 0,
    // and declares a local-scope char c initialized to 'x'.
    for (char c=((i=0),'x'); i<10; ++i) {
        // ...
    }
but I would never do this; it's non-intuitive and ugly.
(Besides, in real code I almost never use comments that explain
WHAT instead of WHY.)

Note that this is not the same as
    for (i=0; i<10; ++i) {
        char c='x';
        // ...
    }
because the initialization should only happen once (and the
value of c should be able to change from one iteration to the
next).

One obvious answer is to perform the initialization first:
    i=0;
    for (char c='x'; i<10; ++i) {
        // ...
    }
but this is still non-intuitive; since the initialization is
before the for() loop, it's not obvious that this is the
control variable initialization. In situations like this, I
usually settle for an extra set of braces:
    {   char c='x';
        for (i=0; i<10; ++i) {
            // ...
    }   }

What I really want is something like this (syntax could vary):
    for (i=0, char c='x'; i<10; ++i)
        // ...
    }

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1998/11/25
Raw View
On 24 Nov 98 13:37:54 GMT, Juan Carlos Gil Montoro <jcgil@gmv.es> wrote:

>Now I'm wondering whether the standard allows
>to define two (or more) variables local to a for
>loop but *of different types*, in the line of:
>
>for(char c = 'x', int i = 0 ; i < 10 ; i ++)
>   cout << i << "-" << c << endl ;
>
>Is there a way of doing that ?

No.  But you make make a local struct.  It is a little cumbersome
most times though.

void f()
{
     struct Var
     {
          char c;
          int i;

          Var(char c_increment, int i_increment) : c(), i() { };
          Var& operator++();
          operator bool() const;
     }

     for (Var v(0,1); v; ++v) ;
}


--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/11/25
Raw View
Siemel Naran wrote:

[...]

> No.  But you make make a local struct.  It is a little cumbersome
> most times though.
>
> void f()
> {
>      struct Var
>      {
>           char c;
>           int i;
>
>           Var(char c_increment, int i_increment) : c(), i() { };
>           Var& operator++();
>           operator bool() const;
>      }
>
>      for (Var v(0,1); v; ++v) ;
> }

Would the following be legal, too?

for(struct { int i; char c; } vars = { 0, 'x' }; vars.i<10; ++vars.i)
  cout << vars.i << "-" << vars.c << endl;

Maybe even this:

for(struct { int i; char c; } = { 0, 'x' }; i<10; ++i)
  cout << i << "-" << c << endl;



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Juan Carlos Gil Montoro <jcgil@gmv.es>
Date: 1998/11/24
Raw View
The following is legal:

for(int j = 1, i = 0 ; i < 10 ; i ++)
   cout << i << "-" << j << endl ;

Now I'm wondering whether the standard allows
to define two (or more) variables local to a for
loop but *of different types*, in the line of:

for(char c = 'x', int i = 0 ; i < 10 ; i ++)
   cout << i << "-" << c << endl ;

Is there a way of doing that ?

Juan Carlos---
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]