https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117504
Bug ID: 117504
Summary: Incorrect code emitted when using "constexpr
std::array"
Product: gcc
Version: 14.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: arnaud02 at users dot sourceforge.net
Target Milestone: ---
Considering:
#include <algorithm>
#include <array>
#include <iostream>
#include <span>
#include <utility>
auto f(int const n) {
using s_t = std::pair<int, int>;
#if defined(WITH_CONST)
const
#else
// gcc 14 emits incorrect code with "constexpr"
constexpr
#endif
std::array a_vec{s_t{1, 123}};
auto const vec{[&a_vec]() -> std::span<s_t const> { return a_vec; }()};
{
auto const it = std::ranges::find_if(vec, [n](auto const& v) {
std::cout << v.first << ", " << v.second << '\n';
return n >= v.first;
});
if (it != std::ranges::end(vec)) {
return it->second;
}
}
return -1;
}
int main() {
auto const r = f(1);
std::cout << "Found=" << r << " expected=123\n";
}
Using g++ 14.2
>g++ -std=c++20 -O0 -DWITH_CONST ex.cpp
>./a.out
1, 123
Found=123 expected=123
But
>g++ -std=c++20 -O0 exp.cpp
>./a.out
-2120994312, 32766
Found=32766 expected=123
Using "constepxr std::array" instead of "const std::array" results in incorrect
code.
This can be reproduced with: https://godbolt.org/z/Ysoc59179