[Libreoffice] OString += OString + char ... ?

Caolán McNamara caolanm at redhat.com
Mon Feb 28 13:45:36 PST 2011

On Sun, 2011-02-27 at 04:27 -0500, Kevin Hunter wrote:
> Is that + operator expected to work with an O*String and a single character?
> From analysis with Sébastien Le Ray, it looks like what's happening is 
> that the b OString is converted to sal_Char *, and the compiler uses 
> int( '\n' ) (=10) for the + operator.  So, the pointer of "Gobble 
> Wobble" is moved to "ble" before the += operator takes over.

Yeah, looks that way.

> Changing the suspect line to
> a += b + "\n";

Indeed, a OString can be constructed from a "const sal_Char*", which is
just a "const char*", so OString+"\n", gets... 

OString operator+(const OString &, const OString&), and the
OString(const sal_Char*) ctor is used to generate the 2nd OString from
the "\n". In the other case, the OString(sal_Char) ctor is an explicit
one (i.e. a += b + OString('\n') should do the right thing), so no
OString is generated for '\n', so the other direction is tried, and
OString has a operator const sal_Char*().

This is the kind of monstrosity that has the suggestion for "explicit"
to be allowed for conversion operators in C++0x.

Three possibilities I guess, 

1) remove the "explicit", but I'm sure its there due to some other
horribly implicit conversion horror
2) add a operator+(sal_Char), either one that "just works", or one that
raises an error at compile time.
3) remove the const sal_Char* conversion operator (I'm tempted towards
this one)


More information about the LibreOffice mailing list