You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
i.e., it only replaces the outer parentheses by braces and g++ accepts that silently, although adding -Wall does emit warning: left operand of comma operator has no effect [-Wunused-value] for each element in the array. Examining the array after initialization in gdb shows:
BTW, is there currently a way to do the equivalent of constexpr std::size_t ROWS = 3? I used ROWS: const i32 = 3 but I don't think that equivalent, plus if I'm not mistaken the const is superfluous.
Also, I tried to write a for loop to iterate but couldn't quite get the syntax right.
Finally, I tried replacing the std::array by std::vector and cppfront -p accepted it as valid, but then g++ did not like the parenthetical initializations.
BTW, is there currently a way to do the equivalent of constexpr std::size_t ROWS = 3? I used ROWS: const i32 = 3 but I don't think that equivalent, plus if I'm not mistaken the const is superfluous.
I do ROWS: constant<3> = ():
constant: <V: _> type == std::integral_constant<decltype(V), V>;
Also, I tried to write a for loop to iterate but couldn't quite get the syntax right.
It's for range next expression do (element) statement
or for range do (element) statement.
You can find how to extract the grammar at #358.
I was just trying to cook up a hack to get Cppfront to lower a braced-init-list.
Multidimensional std::array seems to be an outlier in that it requires an extra braced-init-list.
See https://cpp2.godbolt.org/z/nYbcbP37T.
One way could be to implement a feature similar to P2163 in cpp2. cpp2 does not have the notion of braced-init list, native tuples could replace that while giving more features. Another alternative could be to implement simple array literals like other languages have, also recommended in #424.
this is the cpp version of the function I was trying to replace
void tag_invoke(boost::json::value_from_tag, boost::json::value &jv,
file_data const &v) {
auto file_time_tp =
std::chrono::time_point_caststd::chrono::system_clock::duration(
v.timestamp - std::filesystem::file_time_type::clock::now() +
std::chrono::system_clock::now());
std::time_t file_time_t = std::chrono::system_clock::to_time_t(file_time_tp);
I get these warnings instead, the code compiles though which I think is dangerous!
main2.cpp2:269:10: warning: left operand of comma operator has no effect [-Wunused-value]
jv = { ("directory_name", v.directory_name),
^~~~~~~~~~~~~~~~
main2.cpp2:270:3: warning: left operand of comma operator has no effect [-Wunused-value]
("extension", v.extension),
^~~~~~~~~~~
main2.cpp2:271:3: warning: left operand of comma operator has no effect [-Wunused-value]
("size", v.size),
^~~~~~
main2.cpp2:272:3: warning: left operand of comma operator has no effect [-Wunused-value]
("timestamp", std::move(timestamp_str)) };
^~~~~~~~~~~
main2.cpp2:409:1: warning: non-void function does not return a value [-Wreturn-type]
}
Yeah, that's double balanced parentheses,
where the outer one is an initializer list,
and the inner one is a primary-expression.
This can be confusing due to Cpp2's overloading of parenthesis and lack of distinct braced-init-list syntax (#542).
This particular formulation is worth making ill-formed when non-last expressions are not discarded (e.g. ((_ = a, b)) is OK).
Ah, but requiring _ = a in (_ = a, b) would prevent you from invoking a comma operator.
I know that Cpp2 won't allow authoring operator,, but what about calling it?
There's also the option of lowering all expression lists within an initializer as braced-init-lists.
If you really wanted to use the comma operator, you can defer it to a IIFE: :std::array<int, 2> = (x, :() (y, z)$;()).
Activity
JohelEGP commentedon Aug 2, 2023
See #542.
Currently, you only get braced-init-list in a few select places.
JohelEGP commentedon Aug 2, 2023
I do
ROWS: constant<3> = ():It's
for range next expression do (element) statementor
for range do (element) statement.You can find how to extract the grammar at #358.
JohelEGP commentedon Aug 2, 2023
This is how you can make it work today (https://cpp2.godbolt.org/z/815eovG3T):
JohelEGP commentedon Aug 2, 2023
I was just trying to cook up a hack to get Cppfront to lower a braced-init-list.
Multidimensional
std::arrayseems to be an outlier in that it requires an extra braced-init-list.See https://cpp2.godbolt.org/z/nYbcbP37T.
AbhinavK00 commentedon Aug 2, 2023
One way could be to implement a feature similar to P2163 in cpp2. cpp2 does not have the notion of braced-init list, native tuples could replace that while giving more features. Another alternative could be to implement simple array literals like other languages have, also recommended in #424.
farmerpiki commentedon Sep 18, 2023
this also happens with boost::json
tag_invoke_v2_nonrepresentable: (copy _: boost::json::value_from_tag, inout jv: boost::json::value,
v: file_data) = {
file_time_tp: = std::chrono::time_point_caststd::chrono::system_clock::duration(
v.timestamp - std::filesystem::file_time_type::clock::now() +
std::chrono::system_clock::now());
file_time_t: std::time_t = std::chrono::system_clock::to_time_t(file_time_tp);
}
this is the cpp version of the function I was trying to replace
void tag_invoke(boost::json::value_from_tag, boost::json::value &jv,
file_data const &v) {
auto file_time_tp =
std::chrono::time_point_caststd::chrono::system_clock::duration(
v.timestamp - std::filesystem::file_time_type::clock::now() +
std::chrono::system_clock::now());
std::time_t file_time_t = std::chrono::system_clock::to_time_t(file_time_tp);
std::string timestamp_str =
fmt::format("{:%FT%T}", fmt::localtime(file_time_t));
jv = {{"directory_name", v.directory_name},
{"extension", v.extension},
{"size", v.size},
{"timestamp", timestamp_str}};
}
I get these warnings instead, the code compiles though which I think is dangerous!
main2.cpp2:269:10: warning: left operand of comma operator has no effect [-Wunused-value]
jv = { ("directory_name", v.directory_name),
^~~~~~~~~~~~~~~~
main2.cpp2:270:3: warning: left operand of comma operator has no effect [-Wunused-value]
("extension", v.extension),
^~~~~~~~~~~
main2.cpp2:271:3: warning: left operand of comma operator has no effect [-Wunused-value]
("size", v.size),
^~~~~~
main2.cpp2:272:3: warning: left operand of comma operator has no effect [-Wunused-value]
("timestamp", std::move(timestamp_str)) };
^~~~~~~~~~~
main2.cpp2:409:1: warning: non-void function does not return a value [-Wreturn-type]
}
JohelEGP commentedon Sep 18, 2023
Yeah, that's double balanced parentheses,
where the outer one is an initializer list,
and the inner one is a primary-expression.
This can be confusing due to Cpp2's overloading of parenthesis and lack of distinct braced-init-list syntax (#542).
This particular formulation is worth making ill-formed when non-last expressions are not discarded (e.g.
((_ = a, b))is OK).JohelEGP commentedon Oct 5, 2023
Ah, but requiring
_ = ain(_ = a, b)would prevent you from invoking a comma operator.I know that Cpp2 won't allow authoring
operator,, but what about calling it?JohelEGP commentedon Jan 9, 2024
There's also the option of lowering all expression lists within an initializer as braced-init-lists.
If you really wanted to use the comma operator, you can defer it to a IIFE:
:std::array<int, 2> = (x, :() (y, z)$;()).