lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Converting std::string to wxString


From: Vadim Zeitlin
Subject: Re: [lmi] Converting std::string to wxString
Date: Mon, 6 Feb 2017 18:50:26 +0100

On Mon, 6 Feb 2017 16:57:16 +0000 Greg Chicares <address@hidden> wrote:

GC> Oh...and wxString handles UTF-8. Maybe in C++37.

 Unfortunately it's worse than this. wxString tries to make all the
m(b?)illions lines of legacy, not Unicode-aware, code using it written
since ~25 years (this includes lmi BTW) to continue to compile and work
while still supporting Unicode too. This requires some dirty hacks ^W^W a
lot of inventiveness which would never be supported by any standard class.

GC> > to write "wxString(...)" neither, so the last added line above could be
GC> > rewritten either as
GC> > 
GC> >   return wxString(parser_diagnostics);
GC> > 
GC> > or even as just
GC> > 
GC> >   return parser_diagnostics;
GC> 
GC> Yes, that's best.

 FWIW I agree.

GC> >  I'm not sure if the latter is not too magic and I'd understand if you
GC> > preferred to keep "wxString(...)" for clarity, but I'd definitely remove
GC> > ".c_str()" which is just superfluous.
GC> > 
GC> >  Finally, for completeness, notice that the situation in the other
GC> > direction is, unfortunately, not symmetric as we can't add a ctor from
GC> > wxString to std::string class, so you still have to explicitly call
GC> > wxString::ToStdString() to convert a wxString to std::string.
GC> 
GC> We've been having this discussion for ages, and I never can remember
GC> what the rules are.

 This is probably because they did change since 2005 or thereabouts, when
we might have had this discussion for the first time. But they only changed
once and this was many years ago already, so hopefully it shouldn't be so
complicated to remember them now.

GC> But let me ask this: if I just do whatever seems simplest to me, can I
GC> trust the compiler to give a warning whenever I've oversimplified?
GC> "Code for simplicity, but don't commit anything that doesn't compile"
GC> is a rule I can memorize.

 In principle, yes, but in practice I asked you several times to add
ToStdString() calls because I don't always use a wx built with
wxUSE_STL==1, as you do (just because it's more convenient for me to reuse
the same wx build for all projects I'm working on instead of having a
separate build for each of them... I know I'm being lazy here and I'm
afraid it's not even the kind of laziness that is a virtue of a great
programmer). So omitting ToStdString() would compile (and work) for you,
but wouldn't compile for me.

 I actually do have a solution for this: we've been asked many times (maybe
even by you too?) to remove implicit wxString conversions to "const char*"
and "const wchar_t*" in the default build or to "std::string" and
"std::wstring" in the wxUSE_STL==1 one. We can't do it by default because
it would break too much existing code, especially in !wxUSE_STL case, but I
agree that it would be a good idea to allow opting in to this safer
behaviour, e.g. allow predefining some wxDISABLE_IMPLICIT_WXSTRING_CONV to
ensure that the code implicitly converting wxString to std::string doesn't
compile. If we did this, then your rule above would work without any
exceptions -- but only if/when you updated to the latest wxWidgets version.

 Please let me know if this is something you'd be ready to do. Otherwise
I'm afraid I can't provide any rule simpler than "do nothing when
converting std::string to wxString and use ToStdString() in the other
direction".

 Regards,
VZ


reply via email to

[Prev in Thread] Current Thread [Next in Thread]