Open jmafc opened 1 year ago
See #542. Currently, you only get braced-init-list in a few select places.
BTW, is there currently a way to do the equivalent of
constexpr std::size_t ROWS = 3
? I usedROWS: const i32 = 3
but I don't think that equivalent, plus if I'm not mistaken theconst
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.
This is how you can make it work today (https://cpp2.godbolt.org/z/815eovG3T):
main: () = {
board: std::array<std::array<u8, 3>, 3> = (
:std::array<u8, 3> = ('O', 'X', 'O'),
:std::array<u8, 3> = (' ', 'X', 'X'),
:std::array<u8, 3> = ('X', 'O', 'O')
);
}
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.
#include <array>
#define INIT(...) {__VA_ARGS__}
main: () = {
board: std::array<std::array<u8, 3>, 3> = (INIT(
INIT('O', 'X', 'O'),
INIT(' ', 'X', 'X'),
INIT('X', 'O', 'O')
));
}
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 also happens with boost::json
tag_invoke_v2nonrepresentable: (copy : boost::json::value_from_tag, inout jv: boost::json::value,
v: file_data) = {
file_time_tp: = std::chrono::time_point_cast
timestamp_str: std::string = fmt::format("{:%FT%T}", fmt::localtime(file_time_t));
jv = (("directory_name", v.directory_name),
("extension", v.extension),
("size", v.size),
("timestamp", timestamp_str));
}
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_cast
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]
}
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?
Describe the bug Initialization of a two-dimensional std::array appears to be broken.
To Reproduce Steps to reproduce the behavior:
cppfront compiler v0.2.1 Build 8724:0846 g++ (Debian 12.2.0-14) 12.2.0
i.e., it only replaces the outer parentheses by braces and g++ accepts that silently, although adding
-Wall
does emitwarning: left operand of comma operator has no effect [-Wunused-value]
for each element in the array. Examining the array after initialization ingdb
shows:which explains the actual output.
BTW, is there currently a way to do the equivalent of
constexpr std::size_t ROWS = 3
? I usedROWS: const i32 = 3
but I don't think that equivalent, plus if I'm not mistaken theconst
is superfluous. Also, I tried to write afor
loop to iterate but couldn't quite get the syntax right. Finally, I tried replacing thestd::array
bystd::vector
andcppfront -p
accepted it as valid, but then g++ did not like the parenthetical initializations.