[BUG] constexpr initialization for std::array
MaxSagebaum opened this issue · comments
It seems that constexpr initalization is generated the wrong way.
var : std::array<char, 5> == "abcd";
is lowered to
std::array<char,5> constexpr var = "abcd";
which results into the error:
main.cpp2:8:34: error: no viable conversion from 'const char[5]' to 'const std::array<char, 5>'
The correct lowering in this case would be:
std::array<char, 5> constexpr var {"abcd"};
this version, without the constexpr
, is used for var : std::array<char, 5> = "abcd";
.
Reproducer:
https://cpp2.godbolt.org/z/KbPvxPdoq
I'm not sure if converting from a string literal "abcd"
to std::array<char>
is intentionally supported or not. My guess is this syntax for non-constexpr init:
var2 : std::array<char, 5> = "abcd";
only happens to work because of the cppfront rules of turning that into {"abcd"}
.
But I think the Cpp2 idiom for initialising a std::array<char>
is:
var1 : std::array<char, 5> = ('a', 'b', 'c', 'd', '\0');
var2 : std::array<char, 5> == ('a', 'b', 'c', 'd', '\0'); // constexpr
That second var2
line lowers to:
std::array<char,5> constexpr var2{ 'a', 'b', 'c', 'd', '\0' };
Thanks!
only happens to work because of the cppfront rules of turning that into
{"abcd"}
.
Then ==
should be consistent with that, I think. Fixing...
Thanks.