C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Initial draft for C++: Uniform Initialization for Multiple Variables

From: Julien Villemure-Fréchette <julien.villemure_at_[hidden]>
Date: Fri, 25 Oct 2024 17:05:19 -0400
You can already use commas to define and initialize multiple variables that share the same "non-declarator" type (for pointer, reference or array types, the declarator for such variables is specified independently). Declaring multiple variables in this way is identical to declaring each one in its own declaration statement in the same order. In case you want to initialize all variables to the same value, initialize the first variable by any means you want, then copy initialize the remaining using the first variable. For instance:

```
SomeType var1{}, var2 = var1, var3 = var1, var4 = var1;

```
This is identical to:
```
SomeType var1{};
SomeType var2 = var1;
SomeType var3 = var1;
SomeType var4 = var1;
```
For pointer types, you need to prefix `*` to each variables.


On August 12, 2024 7:44:35 a.m. EDT, Amber Bhardwaj via Std-Proposals <std-proposals_at_[hidden]> wrote:
>*Hello CppCommunity,*
>
>
>*My name is Amber Bhardwaj. It's been almost a decade since I started using
>C++. I got an idea that I want to present in front of this cpp community. I
>am not sure how good this idea is for others! But I feel this feature
>should be present so kindly review it and suggestions are most welcome. *
>
>*1. Problem StatementC++ currently requires separate lines for declaring
>and initializing multiple variables of the same type. This can become
>verbose and cumbersome, especially for simple cases where all variables
>share the same value.2. Proposed SolutionIntroduce a new syntax for uniform
>initialization of multiple variables within curly braces:*
>>
>> *type {var1, var2, ..., varn} = value;*
>
>
>
>
>*where: - type specifies the data type of the variables being declared.-
>var1, var2, ..., varn are comma-separated names of the variables.- value is
>the expression used to initialize all variables.Example:*
>>
>> *int {x, y, z, p} = 0; *
>
>
>
>
>
>
>
>
>
>
>
>
>*This code declares and initializes four integer variables x, y, z, and p
>to the value 0.3. Advantages - Improved code readability: The syntax is
>more concise and easier to maintain, especially for initializing multiple
>variables with the same value.- Reduced boilerplate: Eliminates the need
>for separate lines for each variable declaration and initialization.-
>Potential for pattern matching: This syntax might open doors for future
>language features like pattern matching in the future.4. Disadvantages -
>Potential for parser ambiguity: Careful consideration is needed to avoid
>ambiguity with existing syntax.- Backward compatibility: This feature could
>introduce minor compatibility issues for existing code.- Limited
>applicability: This approach is primarily useful for simple initialization
>of variables with the same type.5. Design Considerations - Should the
>syntax be limited to a single initializer (e.g., only constants) or allow
>for expressions?- How can we ensure clarity and avoid conflicts with
>existing destructuring syntax?- Can this syntax be extended to handle
>different data types with specific limitations?6. Prior ArtSimilar syntax
>exists in other programming languages like Python and JavaScript. In C++,
>structured bindings with tuples offer a partial solution but require more
>complex code.7. Future Directions - I encourage discussion and feedback
>from the C++ community via the std-proposals mailing list.- Based on
>community input, we plan to refine the proposal to address potential
>issues.- I will present the final proposal to the C++ standards committee
>for consideration.8. ConclusionThis proposal introduces a new syntax for
>initializing multiple variables in C++. I believe this feature would
>enhance code readability and conciseness while maintaining the core
>strengths of the language. I look forward to engaging with the community
>and working towards potential implementation.*
>
>
>*Thanks & Regards,Amber Bhardwaj*

Received on 2024-10-25 21:05:30