From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,196864e6c216ca4f X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-09-25 10:23:46 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: taashlo@sandia.gov (Tad Ashlock) Newsgroups: comp.lang.ada Subject: Re: How to Emulate C++ Macro with Static Local Variable? Date: 25 Sep 2003 10:23:44 -0700 Organization: http://groups.google.com/ Message-ID: <5917f4d0.0309250923.5d5a9460@posting.google.com> References: NNTP-Posting-Host: 134.253.26.6 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1064510625 26523 127.0.0.1 (25 Sep 2003 17:23:45 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 25 Sep 2003 17:23:45 GMT Xref: archiver1.google.com comp.lang.ada:42910 Date: 2003-09-25T17:23:45+00:00 List-Id: Stephen Leake wrote in message news:... > 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