4

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 ?

1

1 Answer 1

7

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

I will add your testcase to the bug.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure if this is the same bug. To me, it looks more like this issue is related to initializer_list: coliru.stacked-crooked.com/a/0e0258b3a1a034dc But I admit, it might just be a weird effect of the bug you've linked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.