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

Marc Mutz marc.mutz at kdab.com
Fri Dec 25 10:35:02 CET 2015

On Friday 25 December 2015 01:16:54 Kevin Kofler wrote:
> Matthew Woehlke wrote:
> >   auto result = QString{a + b};
> IMHO, that's just a complicated and ugly way to write:
>   QString result = a + b;
> There is no way the variable can be uninitialized if you initialize it
> right there in the declaration.

Actually, the point here is that this construction with an explicit type name 
is rare and signals an explicit type conversion, or a type constructor.

In the AAA style I know, this should have been a static_cast<QString>(a+b), 
btw, because it is a conversion.

The {}, in general, is preferred because it prevents narrowing conversions.

There are a lot of problems with the C declarator syntax (implicit narrowing 
conversions, hard-to-parse for humans, "most vexing parse", non-uniformity), 
and auto and uniform init and template aliases are the fixes. If you don't use 
them, then you're stuck in the C declarator syntax. You need to use a 
radically different syntax to get the nicer rules.

And yes, I also abhor QString s{a}, but in five years time, every C++ 
programmer's eyes will have become used to it. They must, if they don't want 
to be stuck in the past.


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