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,bc1361a952ec75ca X-Google-Attributes: gid103376,public X-Google-Thread: 1014db,582dff0b3f065a52 X-Google-Attributes: gid1014db,public X-Google-Thread: 109fba,582dff0b3f065a52 X-Google-Attributes: gid109fba,public X-Google-ArrivalTime: 2001-08-14 05:45:26 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!grolier!nerim.net!norfair.nerim.net!not-for-mail From: "Bertrand Augereau" Newsgroups: comp.lang.ada,comp.lang.c,comp.lang.c++ Subject: Re: How Ada could have prevented the Red Code distributed denial of service attack. Date: Tue, 14 Aug 2001 14:51:35 +0200 Organization: Nerim -- xDSL Internet Provider Message-ID: <9lb6h4$6e9$1@norfair.nerim.net> References: <3b690498.1111845720@news.worldonline.nl> <9kbu15$9bj@augusta.math.psu.edu> <9kbvsr$a02@augusta.math.psu.edu> <3B69DB35.4412459E@home.com> <3B6F312F.DA4E178E@home.com> <23lok9.ioi.ln@10.0.0.2> <3B70AB15.35845A98@home.com> <3B721FF5.B7D854F6@home.com> NNTP-Posting-Host: aboukir-101-2-1-inutero.adsl.nerim.net X-Trace: norfair.nerim.net 997793124 6601 62.4.19.69 (14 Aug 2001 12:45:24 GMT) X-Complaints-To: abuse@nerim.net NNTP-Posting-Date: Tue, 14 Aug 2001 12:45:24 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 Xref: archiver1.google.com comp.lang.ada:11909 comp.lang.c:74139 comp.lang.c++:82439 Date: 2001-08-14T14:51:35+02:00 List-Id: > type Colour is ( Red, Green, Blue ); > > Background : Colour := Red; > > ... > begin > -- Debug : > Put_Line("Background = " & Colour'Image(Background)); > > One Put_Line statement using the Colour'Image() attribute allows me to > conveniently print out the value of the enumerated type in human > readable terms. To do this in C++, you'd either choose between printing > the "integer" value of it, and going back to the include/declaration to > figure out what it was, _or_ you'd have to do: > > static char str_colours[] = { "red", "green", "blue" }; > enum { red, green, blue } colour; > colour c = blue; > > printf("colour = %s\n",str_colours[c]); > > ... which only works if your enums start from zero. Otherwise, you additionally > have to map it : > > static char str_colours[] = { "red", "green", "blue" }; > enum { red=100, green=200, blue=300 } colour; > colour c = blue; > > // the following is hopeless, and won't work : > printf("colour = %s\n",str_colours[c]); > > before you object to that, let me add that enums are used with all > sorts of weird values -- not all of them start from zero. In fact, if > they all did, you'd have a harder time detecting runtime errors due to > mismatched use of enums. I purposely choose different starting values > for C/C++ enums for that reason. With templates, you can evaluate this mapping at compile time, enum A {a=12,b=38,c=95}; template const char* GetImage (void); template<> const char* GetImage (void) { return "a"; } template<> const char* GetImage (void) { return "b"; } template<> const char* GetImage (void) { return "c"; } And with the help of the preprocessor, you might get even more terse syntax. But you Ada programmers like it verbose, don't you ;-) I didn't want to state that this is superior to Ada 'Image approach, which is quite useful for quick hacks and debugging purpose, but I guess you underevaluate the true power of C++ (especially in metaprogramming)