Playing with constexpr I had a strange situation that I reduced to a simple C++ example that works fine on VS2019 and GCC (tested on godbolt.org) but fails on the latest VS2022 (ver 17.11.0)
constexpr bool contains(const char strz[], char ch) noexcept {//search a char in a 0-terminated string
for(auto*c=strz; *c; ++c){
if(*c == ch){
return true;
}
}
return false;
}
struct S{
static constexpr const char* const arr[] = {
"abc",
"*12" // <-- comment this to get a compile error on VS2022
};
};
template<typename S>
class Cls{
public:
static constexpr auto func() noexcept {
for(const auto& s: S::arr){
if(contains(s, '*')){
return true;
}
}
return false;
}
};
int main(){
constexpr auto r = Cls<S>::func();// <-- remove constexpr to have it work also with only 1 element in "arr"
return r;
}
The error is: error C2131: expression did not evaluate to a constant
It is also very strange (to me) that it fails when the array “arr” has only one element but works when it has more!
Looks like it is constexpr-related because it works also after removing “constexpr” from main(), even in the case with only one element in “arr”
Did I missed something? Or there is a bug in the latest VS2022?
Can somebody please confirm/infirm the results I have with VS2022 (ver 17.11.0)?
9