comp.lang.ada
 help / color / mirror / Atom feed
From: taashlo@sandia.gov (Tad Ashlock)
Subject: Re: How to Emulate C++ Macro with Static Local Variable?
Date: 25 Sep 2003 10:23:44 -0700
Date: 2003-09-25T17:23:45+00:00	[thread overview]
Message-ID: <5917f4d0.0309250923.5d5a9460@posting.google.com> (raw)
In-Reply-To: uhe310yvm.fsf@nasa.gov

Stephen Leake <Stephe.Leake@nasa.gov> wrote in message news:<uhe310yvm.fsf@nasa.gov>...
> taashlo@sandia.gov writes:
[snip]
> Just declare each "hidden static" variable directly, and call 'bar'. 
> 
> Yes, it's a few more lines of code, but it's worth it!
> 
> > for (int i = 0; i < 55; ++i) {
> >    switch (wozit()) {
> >       case 1: BAR(10); break;
> >       case 2: BAR(20); break;
> >       case 3: BAR(30); break;
> >    }
> > }
> 
> This would become:
> 
> package foo
>     t_1 : s;
>     t_2 : s;
>     t_3 : s;
> 
> procedure Something is
> begin
>     for i in 1 .. 54 loop
>        case wozit is
>        when 1 =>
>            bar (t_1, 10);
>        when 2 =>
>            bar (t_2, 20);
>        when 3 =>
>            bar (t_3, 30);
>        end case;
>     end loop;
> end Something;
> 
> end foo;
> 
> You may complain this is using "ugly global variables", and it is. So is the
> C code. Using macros to hide doesn't really make it prettier :).

If the problem with this was just the ugliness, then I wouldn't have a
problem! :-)  The real problem with this is that a typo (e.g. t_2
instead of t_3) would be impossible for the compiler to catch, and
would produce a subtle bug where the subsequent behavior of a call
to bar() would depend on which call executed first.

The C++ implementation doesn't have *this* problem.

> A better way is to make 't' part of a state object that is passed in
> to Something, but that's left as an exercise for the reader.

This (I believe) would have the same problem I noted above.

Thank you,
Tad



      reply	other threads:[~2003-09-25 17:23 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-24 22:06 How to Emulate C++ Macro with Static Local Variable? taashlo
2003-09-24 23:44 ` Robert I. Eachus
2003-09-25  3:27   ` Hyman Rosen
2003-09-25  4:53     ` tmoran
2003-09-25 15:57       ` Robert I. Eachus
2003-09-25 19:09         ` tmoran
2003-09-29 14:57           ` taashlo
2003-09-29 18:12             ` Jeffrey Carter
2003-09-29 18:51               ` taashlo
2003-09-30  1:32                 ` Jeffrey Carter
2003-09-30  1:54                   ` tmoran
2003-09-30 13:02                   ` taashlo
2003-09-30 20:25                     ` Jeffrey Carter
2003-09-30  2:45             ` Robert I. Eachus
2003-09-30  3:24               ` tmoran
2003-09-25 13:43     ` Stephen Leake
2003-09-25 12:59   ` Tad Ashlock
2003-09-25 16:11     ` Robert I. Eachus
2003-09-25 18:13       ` Randy Brukardt
2003-09-25 23:40         ` Robert I. Eachus
2003-09-25 11:54 ` Gautier
2003-09-25 16:14   ` Robert I. Eachus
2003-09-25 18:06   ` Tad Ashlock
2003-09-28  8:42     ` Gautier Write-only
2003-09-28 20:06       ` Jeffrey Carter
2003-09-29  2:13         ` Gautier Write-only
2003-09-25 13:41 ` Stephen Leake
2003-09-25 17:23   ` Tad Ashlock [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox