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 06:48:13 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!hammer.uoregon.edu!skates!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: How to Emulate C++ Macro with Static Local Variable? Date: 25 Sep 2003 09:41:01 -0400 Organization: NASA Goddard Space Flight Center (skates.gsfc.nasa.gov) Message-ID: References: NNTP-Posting-Host: anarres.gsfc.nasa.gov Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: skates.gsfc.nasa.gov 1064497430 21795 128.183.235.92 (25 Sep 2003 13:43:50 GMT) X-Complaints-To: usenet@news.gsfc.nasa.gov NNTP-Posting-Date: 25 Sep 2003 13:43:50 GMT User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 Xref: archiver1.google.com comp.lang.ada:42893 Date: 2003-09-25T13:43:50+00:00 List-Id: taashlo@sandia.gov writes: > What happens is that when you do this: > > BAR(10); > > it expands to this: > > if (1) { > static s t_; > bar(&t_, 10); > } else ((void)0); > > This, in effect, creates a (hidden) static local variable for each call > to bar(). This static variable (actually a structure, t_) is used to > cache information for each unique call to bar() such that subsequent > calls from that location can be processed *much* faster. 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 :). 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. -- -- Stephe