Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing constexpr changes values of array on gcc

When trying to answer to a question and create a constexpr std::array, I wrote

// Own implementation as std::array::operator[] is not constexpr :/
template <typename T, std::size_t N>
struct array
{
    constexpr T& operator[] (std::size_t index) { return data[index];}
    constexpr const T& operator[] (std::size_t index) const { return data[index];}

    constexpr std::size_t size() const { return N; }

    T data[N];
};

constexpr array<std::size_t, 1001u> make_bottle_count()
{
    array<std::size_t, 1001u> a = {{0, 1, 2, 3, 4, 1, 2, 1, 2, 3, 1}};
    for (int i = 11; i != a.size(); ++i) {
        a[i] = 1 + std::min({a[i - 1], a[i - 5], a[i - 7], a[i - 10]});
    }
    return a;
}

int main() {
    // Change constexpr to const make gcc returns expected result
    constexpr auto bottle_count = make_bottle_count(); 

    std::cout << bottle_count[17] << std::endl;   // expect 2
    std::cout << bottle_count[65] << std::endl;   // expect 7
    std::cout << bottle_count[1000] << std::endl; // expect 100
}

but gcc gives unexpected results (2 2 2) whereas clang gives expected result.

Live demo

Removing constexpr (or replacing by const) for bottle_count gives expected results for both.

Do I invoke UB, or it is a compiler(gcc) bug ?

like image 528
Jarod42 Avatar asked Mar 15 '23 04:03

Jarod42


1 Answers

It is a gcc bug, see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67104

I will add your testcase to the bug.

like image 187
octoploid Avatar answered Mar 26 '23 12:03

octoploid