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@lists.isocpp.org>
On Behalf Of Amber Bhardwaj via Std-Proposals
Sent: Monday, August 12, 2024 13:45
To: std-proposals@lists.isocpp.org
Cc: Amber Bhardwaj <amber.bhardwaj10@gmail.com>
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:
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
4. Disadvantages
5. Design Considerations
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
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