OUString is mutable?

David Tardon dtardon at redhat.com
Mon Oct 1 00:49:13 PDT 2012


Hi,

On Fri, Sep 28, 2012 at 04:05:35PM +0200, Noel Grandin wrote:
> 
> On 2012-09-28 16:00, Caolán McNamara wrote:
> >On Fri, 2012-09-28 at 14:17 +0200, Noel Grandin wrote:
> >>you can do this:
> >>
> >>      void f(OUString s) {
> >>           s = "2";
> >>      }
> >>
> >>      OUString s = "1";
> >>      f(s);
> >>      cout << s; // will print "2"
> >That will print "1" not "2".
> >
> >Maybe you meant
> >
> >void f(OUString& s) {
> >   s = "2";
> >}
> >
> >OUString s = "1";
> >f(s);
> >cout << s; // will print "2"
> >
> >but that's perfectly reasonable.
> >
> >C.
> >
> 
> 
> Yeah, that's what I meant.
> But that's also what I have a problem with.
> It means that any OUString field or variable is effectively mutable,
> which makes the difference between it and OUStringBuffer boil down
> to the presence of the nCapacity field.

I find it perfectly reasonable that a variable of a value type (as
opposed to polymorphic type) is assignable. In fact, I would be
surprised if it were not. Value types are supposed to mimic the behavior
of primitive types; that is why copy constructor and operator= are
created by the compiler unless one disables them. You are not surprised
that

int i(1);
i = 2;

or

char const* s("1");
s = "2";

works, are you? So why

rtl::OUString s("1");
s = "2":

should be different? C++ already has a way to express that a variable is
not mutable: the 'const' modifier.

Last, but not least, working assignment is requirement for many, if not
all, STL containers.

D.


More information about the LibreOffice mailing list