[Development] RFC: more liberal 'auto' rules?

Ziller Eike Eike.Ziller at theqtcompany.com
Tue Dec 22 13:42:06 CET 2015

> On Dec 22, 2015, at 1:28 PM, Marc Mutz <marc.mutz at kdab.com> wrote:
> 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++.

I’m actually not arguing against the use of auto in principle. But I’m arguing against introducing auto because it (supposedly?) makes the code safe(r). (That context has unfortunately been cut off from the mail at some point.)
C++ is complicated, and type deduction and its results are no exception.
If you know what you are doing, auto can make code more readable and easier to write, especially with templated code.
When it makes code more readable or not tends to be pretty subjective it seems, so IMO someone just has to decide :)

> 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...

Not sure what you want to say with this analogy.
Anyhow, discussions about pros and cons are still necessary. I’m sure that there are laws in Germany that supposedly increase security but effectively don’t.
I would require that the decision to introduce a law requiring the use of seat-belts was based on extensive studies on how seat belts increase or decrease the security of a car’s occupants, and how they need to be designed to achieve that best. And not on stories at the bar, of how one or the other person was either saved or doomed by a seat belt, which you seem to be reminded of by the discussion here? ;)

Eike Ziller, Senior Software Engineer - The Qt Company GmbH
The Qt Company GmbH, Rudower Chaussee 13, D-12489 Berlin
Geschäftsführer: Mika Pälsi, Juha Varelius, Tuula Haataja
Sitz der Gesellschaft: Berlin, Registergericht: Amtsgericht Charlottenburg, HRB 144331 B

More information about the Development mailing list