Date: Mon, 12 Aug 2024 15:04:07 +0000
The “other suggestions” aren’t quite the same, but they don’t have the following ambiguity:
Suppose you have the following:
NonTrivialType {x, y, z} = NonTrivialType::MakeMyObject(++i);
What happens in this scenario? Does it call “NonTrivialType::MakeMyObject” once and then makes a copy? Is the first argument eligible for elision?
Does it call the function 3 times?
From: Std-Proposals <std-proposals-bounces_at_[hidden]> On Behalf Of Amber Bhardwaj via Std-Proposals
Sent: Monday, August 12, 2024 13:45
To: std-proposals_at_[hidden]pp.org
Cc: Amber Bhardwaj <amber.bhardwaj10_at_[hidden]>
Subject: [std-proposals] Initial draft for C++: Uniform Initialization for Multiple Variables
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 Statement
C++ 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 Solution
Introduce 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 Art
Similar 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. Conclusion
This 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
Suppose you have the following:
NonTrivialType {x, y, z} = NonTrivialType::MakeMyObject(++i);
What happens in this scenario? Does it call “NonTrivialType::MakeMyObject” once and then makes a copy? Is the first argument eligible for elision?
Does it call the function 3 times?
From: Std-Proposals <std-proposals-bounces_at_[hidden]> On Behalf Of Amber Bhardwaj via Std-Proposals
Sent: Monday, August 12, 2024 13:45
To: std-proposals_at_[hidden]pp.org
Cc: Amber Bhardwaj <amber.bhardwaj10_at_[hidden]>
Subject: [std-proposals] Initial draft for C++: Uniform Initialization for Multiple Variables
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 Statement
C++ 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 Solution
Introduce 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 Art
Similar 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. Conclusion
This 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-08-12 15:04:13