[Development] RFC: more liberal 'auto' rules?
Marc Mutz
marc.mutz at kdab.com
Tue Dec 22 13:28:44 CET 2015
On Tuesday 22 December 2015 11:26:40 Ziller Eike wrote:
> We have something similar in Qt:
>
> QString a = "ABC";
> QString b = "abc";
> auto result = a + b;
> a.replace("A", "C");
> qDebug() << result;
>
> prints “CBCabc” instead of “ABCabc” when QT_USE_FAST_OPERATOR_PLUS is
> defined, because result is not a QString in that case.
>
> So funny/unwanted behavior can occur both because one used the wrong
> explicit type, and because one used auto instead of an explicit type.
This is not specific to auto, though:
QStringView v = QLatin1Char('a');
const char *data = str.toLatin1(); // implicit conversion by default
QLatin1String l1 = str.toLatin1();
QVector<int> v = {0, 1, 2};
QVector<int>::iterator it = v.begin();
const QVector<int> v2 = v;
*it = 1;
assert(v2.front() == 0); // boom
It's a general peril when working with C++.
Reminds me of the 1970s when we got a law requiring the use of the seat-belt
in cars in Germany. My uncle used to tell the story how one of his friends
survived a crash because he was ejected from the car since he was _not_
buckled up, just to follow up with the story how another friend was not harmed
in a crash because he _was_ strapped in...
Thanks,
Marc
--
Marc Mutz <marc.mutz at kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
Tel: +49-30-521325470
KDAB - The Qt Experts
More information about the Development
mailing list