[Development] Braces (was Re: Resolving coding style contentions)
Thiago Macieira
thiago.macieira at intel.com
Fri Dec 21 16:06:54 CET 2018
On Friday, 21 December 2018 04:59:34 -02 Alberto Mardegan wrote:
> Hi all!
> Speaking of coding style again, I haven't found any indication in the
> coding guidelines about brace vs parentheses in initializations.
Because it wasn't a possibility before C++11.
I'd say we prefer assignment wherever possible, parens where it works, {}
otherwise. Exception:
= {};
> There are a few cases where they can be used (and I might even be
> forgetting some):
>
> 1) Constructors:
> MyClass(): var(0) {}
> vs
> MyClass(): var { 0 } {}
Ugly -- and NSDMI looks better.
> 2) Member initializations:
> class MyClass
> {
> int m_count = 3;
> vs
> int m_count { 3 };
Assignment looks better.
> 3) Variable initialization
> bool ok = false;
> vs
> bool ok { false };
Ditto.
> 4) Constructor invocations:
> auto *item = new Item("name", parent);
> vs
> auto *item = new Item { "name", parent };
Parens look better.
> or
>
> QString message(QStringLiteral("text"));
> vs
> QString message { QStringLiteral("text") };
> I guess this is not an option:
> QString message { QStringLiteral { "text" } };
Parens look better. And QStringLiteral is a macro, so the third option just
does not compile.
> I'm not mentioning the cases of struct and list initializers, where of
> course braces are the only option. But what is the consensus on the
> cases above?
> Looking at the Qt code, it seems that variant without braces is always
> preferred, but I cannot find it formalized anywhere.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
More information about the Development
mailing list