Re: Why do Glib::ustring::operator[] and at() return values, not references?



On 28 June 2017 at 09:39, Daniel Boles wrote:
But the main drawback I could think of is this: It would change semantics
for anyone currently using auto some_character = non_const_ustring[N], as
the auto would now capture the proxy type, not a gunichar. To get the
latter, the type would have to be explicitly specified to invoke the
conversion operator. Or is there a clever way around this that I don't know?

There's no way around it. There have been proposals for an "operator
auto" that would make it possible to control the deduced type, mostly
for use by expression templates, but nothing that is part of C++ yet.

Another downside of a proxy is it can outlive the string, so this
would be undefined (without some internal complexity to track
lifetimes):

auto c = Glib::ustring("foo")[0];
c = 'b';  // tries to modify the expired temporary

Again, this only happens when using 'auto' because otherwise there's
no attempt to write back into the ustring:

gunichar c = Glib::ustring("foo")[0];
c = 'w'; // just modifies c


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