Date: Wed, 11 Jun 2025 20:29:10 +0200
I’m not sure if there is an official rule in the C++ guidelines, but I have learned to declare variables when they are used. It is a very rare case that I need to declare several variables and then using them all at once. Even less so when all the variables have the same value (a single variable would then suffice).
Most of this practice (the one I am describing) goes back to “almost always const”, i.e. making all variables const if possible. Then, your case does not occur. I would even claim that your proposal encourages bad style, e.g. old C syntax where variables are all declared at the top of the function. Let’s not go back to this!
> On Jun 8, 2025, at 10: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
>
> Rationale:
>
> - Conciseness: This approach reduces redundancy, making the code more compact and readable by eliminating the need to repeat initialization values for each variable.
>
> - 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.
>
>
> Best regards,
> James S.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Most of this practice (the one I am describing) goes back to “almost always const”, i.e. making all variables const if possible. Then, your case does not occur. I would even claim that your proposal encourages bad style, e.g. old C syntax where variables are all declared at the top of the function. Let’s not go back to this!
> On Jun 8, 2025, at 10: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
>
> Rationale:
>
> - Conciseness: This approach reduces redundancy, making the code more compact and readable by eliminating the need to repeat initialization values for each variable.
>
> - 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.
>
>
> Best regards,
> James S.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2025-06-11 18:29:26