views:

424

answers:

7

While I like programming in C++, I hate the idea of:
std::basic_string vs QString vs wxString vs .............
Doesn't the standard string class satisfy the needs for these frameworks? I mean what is wrong with the standard string class?!

Just to emphasize, that below is the important question:
Do you learn "the" string class of the framework in every framework you are going to work with? would you instead stick to the standard string class by trying to adapt it everywhere?

Thanks...

+4  A: 

One reasonable reason (versus unreasonable reasons like "I don't want to learn the Standard Library") is that some libraries wish to retain control over the binary layout, in order to achieve certain kinds of interoperability (such as binary compatibility across versions). An example of this is _bstr_t in the VC++ libraries; it is important for COM purposes that a _bstr_t is represented as a BSTR (since that is what COM needs), so a wrapper built on top of a BSTR is valuable to COM developers.

DrPizza
+11  A: 

The reason for multiple string classes is that the C++ standard was finalized fairly late (in 1998); it then took some time until all systems actually provided a correct C++ library. By that time, all these competing string classes where already written.

In addition, in some cases, people want to inherit from a single base class, which std::string wouldn't do.

Martin v. Löwis
I think you answered the second point implicitly... Just leave it for time, and the standard string will be practically the standard one.
AraK
The trouble is that they did such a poor job. After 10years of deliberating you have a string class that repeats some but not all the std::algorithms, doesn't do unicode, doesn't do copy-on-write and doesn't even play nicely with other standard lib functions that still need a char*.
Martin Beckett
copy-on-write is more expensive/dangerous on multi-processor systems than copy-always. One size does not fit all especially when that one size is everything including the kitchen sink.
jmucchiello
+9  A: 

IMO, std::string isn't old enough to be widespread (Qt and wxWidgets are older than the STL, or at least older than widely available stable and working STLs). Also, std::string is sadly not the best string class there is for everyone, and other frameworks have other needs.

C++ STL is very resource optimized, whereas the Qt string class offer lots of goodies that a committe would never agree on, especially as some want it to be easily implementable on embedded systems and the like.

Marcus Lindblom
Thanks. I see you are answering the second point. Would you learn "the" string class of every platform you work with? Like `QString` and eliminate `std::basic_string`?
AraK
It makes sense to decide on a string type to use in each project, and try to stick to it. Mixing willy-nilly creates pain. So, in a Qt-heavy app I'll probably try to use QString everywhere, even in parts that don't use Qt directly. However, if there was small part Qt GUI and a large part depended on another std::string using library (say a web search engine toolkit), I'd try to use std::string mainly and convert to/from QString only in the GUI part.
Marcus Lindblom
I don't disagree that the C++ committee wouldn't agree on some of QString functionality, but Qt including QString has been used on embedded systems for ages.
Lukáš Lalinský
Speaking as someone who started using C++ in 1990 and lived through the history, the first paragraph of Marcus' answer is right on the money. I can't speak to Qt, but the original STL strings didn't support ref-counted/copy-on-write string duplication, so MFC's CString class was superior to std::string for some purposes in the late 90s - in fact, I wound up cloning the API for the Mac; under the hood, it was actually a ref-counting wrapper around std::string.
Bob Murphy
+6  A: 

One of the main problems with std::string is the lack of Unicode support. Even with std::wstring you only get a container for Unicode code points, but would still have to implement the Unicode-aware functionality.

Also, QString for example is "implicitly shared". This makes it very easy to pass strings around your code in an efficient way. They are actually copied only on write.

Lukáš Lalinský
Things get even more complicated with UTF-8.
Michael E
A: 

IIRC Bjarne Stroustrup deliberately omitted a String class from C++ as he considered it a "rite of passage". All those who learnt C++ were expected to write their own. Certainly at the start of C++ there were no standard libraries and I remember versions from AT&T (which was a preprocessor for C) and the NIH Classes from a very pioneering group at the National Institutes of Health in the US (which also included early collection classes).

peter.murray.rust
A: 

One of the tenants of C++ is "You don't pay for what you don't need." This means there does not need to be a one-size-fits-all string class that every C++ programmer MUST know and (more importantly) must USE. Maybe your project requires thread-safe strings. You can roll your own class. And you always have the option of using the existing std::string.

It just so happens that in most cases std::string is good enough. But when it isn't, aren't you glad you aren't locked into it. Try to roll your own String class in Java and see how long it takes until you are pulling your hair out.

As for your second point, if you are going to fight against a library you've added to your project, why did you add the library to your project in the first place? Part of the decision to use wxWidgets or QT is the acknowledgment that you must embrace its string class in your project (or at least a sizable portion of that project). Just like the decision to a "C" library means putting up with char* buffers and size parameters on all the functions.

So, yes, learn the alternate string class. If you are using a library (and wish to become proficient with it) you can't decide to ignore part of the library just because "it's another string class". That makes no sense.

jmucchiello
That statement is only meant at runtime, it doesn't cost you anything at runtime if the standards committe defined a decent string class - anymore than it slows down windows if more software is available in the store.
Martin Beckett
How is the std::string class not decent?
jmucchiello
+1  A: 

std::string is great... Oh, except that it doesn't have a "Format()" call... And, it doesn't have Split() or Join()... Actually, it doesn't do a lot of things that users of strings in those "inferior" scripting language get to take for granted...

If C++ had the ability to ADD to existing classes (like Objective-C or Ruby) then you probably wouldn't see this...

Also, consider that C++ generally does a better job (than things like Java) at letting you create objects that behave like real native types...

dicroce
But see http://www.boost.org/doc/libs/1_34_1/libs/format/index.html and http://www.boost.org/doc/libs/1_40_0/doc/html/string_algo.html. std::string has already been criticized for having an ungodly amount of member functions. What stops you from ADDING functionality to a class through free functions?
UncleBens
Yes, it has too many methods, but the methods are mostly not useful. It could do better with fewer more useful methods.
Lukáš Lalinský