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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,243dc2fb696a49cd X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!nntp.idg.pl!newsfeed.atman.pl!newsfeed.tpinternet.pl!atlantis.news.tpi.pl!news.tpi.pl!not-for-mail From: "Robert Kawulak" Newsgroups: comp.lang.ada Subject: Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl) Date: Sun, 26 Sep 2004 12:36:41 +0200 Organization: tp.internet - http://www.tpi.pl/ Message-ID: References: <1777528.JKnUEYTOM6@linux1.krischik.com> <1ec946d1.0409230820.455ad242@posting.google.com> <3673998.bj16mkkOu2@linux1.krischik.com> <1700922.2nPlMsa4Ny@linux1.krischik.com> NNTP-Posting-Host: pt253.krakow.cvx.ppp.tpnet.pl X-Trace: nemesis.news.tpi.pl 1096195919 28144 217.99.216.253 (26 Sep 2004 10:51:59 GMT) X-Complaints-To: usenet@tpi.pl NNTP-Posting-Date: Sun, 26 Sep 2004 10:51:59 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original Xref: g2news1.google.com comp.lang.ada:4200 Date: 2004-09-26T12:36:41+02:00 List-Id: Hi! (Sorry for mixing quotations of your post, it was easier for me to respond this way). > In 99.99% of all cases I don't need strings on user defined types. I need > base_string. I can see that I one day might need > base_string, but I have not until now. They made all that funky > design which I just never need. And the best thing is that if you don't need it, you don't have to use it. Yet still there is this 0.01% of cases when this approach is extremely useful because of its flexibility. E.g. when you want a class of case-insensitive strings - you just reuse the basic string template like this: //class defining character traits class my_char_traits: public std::char_traits { //here you just redefine simple comparison //functions like eq() to be case-insensitive }; //case-insensitive string class typedef std::basic_string cis_string; > Would have been so easy, so usefull but it would not have been a template. > The "all is template" religion got in there way. Actually you might not even know that std::string is a typedef of template instantiation and still use it as if nothing had happened. And compare this example to creating completely new string class just because you want this small difference in behaviour - and you have an explanation for making everything a template. :-) Similar results could be achieved using polymorphysm, but the "template religion" has got one great advantage - all the binding is done during compilation, and moreover many functions are inlined (!) - this allows a performance during runtime other approaches couldn't have... > And for that: > > int > string::asInt(); > throw (bad_value); > > and > > explicit > string::string (int value) > throw(); > > are missing. Two functions I am missing on daily basis. Sure, I am an > experienced programmer, I can do that myself - but a proper library should > have all the function one need on a daily basis. std::string doesn't have int conversion functions, because it's not its job to do this. Maybe you don't see the whole idea of the design of C++ standard library. It's goal is to divide responsibility for diffrent features between separate parts of library - that's where it's flexibility and extensibility comes from. > And just for the record: atio is a C function - you need the overhead of > convertion the std::string into a char* and std::strstream seem quite > heavy > weight for just a simple task of convertion a string from and to an int. Actually, the overhead of conversion is virtually none (at least in Stlport implementation). You just call std::string::c_str() which just returns a pointer to the data, and which is also inlined. If you need string-to-int conversion and vice-versa, then it's the job of std::stringstream (std::strstream is obsolete...) - there's a very easy way to do this: /********************************************/ #include using std::string; #include typedef std::ostringstream oss; typedef std::istringstream iss; int i = 4; string MyString("2"); //step-by-step: void f() { //instead of std::string::fromInt(int) oss MyOutputStream; MyOutputStream << i; MyString = MyOutputStream.str(); //instead of std::string::toInt() iss MyInputStream; MyInputStream.str(MyString); MyInputStream >> i; } //f() //same things as one-liners: void g() { MyString = static_cast(oss() << i).str(); iss(MyString) >> i; } //g() //you may also define two simple functions to do this for you: inline string ToString(int i) { return static_cast(oss() << i).str(); } //ToString() inline int ToInt(const string & s) { int i; iss(s) >> i; return i; } //ToInt() void h() { //couldn't be simplier ;-) MyString = ToString(i); i = ToInt(MyString); } //h() /********************************************/ Most compilers will optimise away the initialisations etc., so in this case you don't have to worry about the overhead. But if you really can't live without this ability in the string class, then nothing stands on a way to extend std::string with these two functions using inheritance (note, however, that you will also have to re-implement constructors you want to use. Constructor inheritance is not a part of standard C++ yet). C++ is not that evil, you just have to switch your mind to a different way of thinking to understand it well... ;-) It *is* difficult, but once you feel it, it gives you a lot of abilities. Please don't understand me wrong - I don't claim I'm some kind of C++ guru, and the following is just my general opinion, not an answer for your particular post. Why I think many people think of C++ as so dangerous and letting you easily shoot your foot is that unfortunately not many people know it as well as they should to use it properly. I mean C++ is not a "toy language" - if you want something easy, use Java. But be prepared for many limitations and restrictions (which are there for your own good, but if you exactly know what you are doing, they are inconvenient). I also don't say C++ is better than Ada - it depends on how a programmer is familiar with those languages and how effectively can write good, safe programs using them. Of course C++ forces to learn more to write such programs, but in turn provides you with much more flexibility and usefulness. Best regards, Robert Kawulak