Inconsistency with std::vector
constructors due to braced-init-list
#1271
Unanswered
bluetarpmedia
asked this question in
Q&A
Replies: 1 comment 1 reply
-
In C++ you can also "explicitly" invoke an init-list constructor by saying e.g. std::vector<int> vec({1, 2, 3}); In a similar vein, perhaps something like
could be used in Cpp2? That is, if you want initialiser-list construction, you explicitly pass a list inside the regular constructor brackets |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The following Cpp2:
prints this to stdout:
because of how cppfront emits the constructor calls in C++, which when able invokes the initializer list overload:
Repro on Godbolt.
Can this be improved to prevent the inconsistency and/or avoid ambiguity between a braced-init-list of integers and the two parameters for the
vector
constructor takingcount
andvalue
?One option would be for cppfront to lower the constructor calls using parens instead of braces, which means the following would now be consistent:
and then Cpp2 could offer an alternative syntax for a list of items that would be lowered to a C++ braced-init-list (or, see below, maybe something even better):
This seems similar to Rust's
vec!
macro:But perhaps this is a good chance to fix some issues with C++'s initializer list and implement something like @tcbrindle's
make_vector
helper.Beta Was this translation helpful? Give feedback.
All reactions