C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Compact Initialization Syntax

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 8 Jun 2025 16:24:20 -0400
On Sun, Jun 8, 2025 at 4:08 PM Smith, Jim via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Hi All,
>
> I suggest the following:
>
> float{} a, b, c, d; // Initializes all variables to zero
> float{0.1f} a1, b1, c1, d1; // Initializes all variables to 0.1

`float{}` already has a meaning, as does `float{0.1f}`. Both of those
are expressions, so the syntax is a non-starter.

> Rationale:
>
> - Conciseness: This approach reduces redundancy, making the code more compact and readable by eliminating the need to repeat initialization values for each variable.

Does a fairly rare circumstance *need* to be "concise"? We don't
commonly create 4 variables of the same type in the middle of a
function, and they *certainly* shouldn't be named short things like
`a` or `d1`.

So do we need shorthand syntax for what ought to be a fairly rare
circumstance? After all, people have to learn what this means, and if
they don't see it very often, that makes it much more surprising when
it shows up.

> - Consistency: When initializing multiple variables with the same value, it streamlines the process, avoiding repetitive declarations.
>
> - Clarity of Intent: The use of {} makes it immediately clear that each variable is being initialized to a specific value, without the clutter of individual assignments.
>
> - Cleaner Declarations: Especially in cases with many variables, this approach allows for a cleaner, more organized declaration without compromising on clarity.

Received on 2025-06-08 20:24:33