I am trying to compile some library code and ran into an error; I simplified the example and I have the following MVCE which fails to compile with MSVC 2019 with the error
error C2440: 'return': cannot convert from 'const char *' to 'const char (&)[20]'
static constexpr const char somethingWeird[] = "Well, that's odd...";
void fail() { throw 0; }
// This doesn't work
constexpr const char(&checkNullTerminatedGood(const char(&a)[20]))[20]{
return a[19] == char(0) ? decltype(a)(a) : (fail(), decltype(a)(a));
}
static constexpr const auto somethingElseNew = checkNullTerminatedGood(somethingWeird);
When I convert the ternary operator to a proper if-statement, the code compiles well:
static constexpr const char somethingWeird[] = "Well, that's odd...";
void fail() { throw 0; }
// This works
constexpr const char(&checkNullTerminatedGood(const char(&a)[20]))[20]{
if (a[19] == char(0)) {
return decltype(a)(a);
} else {
return (fail(), decltype(a)(a));
}
}
static constexpr const auto somethingElseNew = checkNullTerminatedGood(somethingWeird);
Is this a bug in MSVC? The first snippet compiles with GCC and Clang.