https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123577

            Bug ID: 123577
           Summary: C++17: Unhelpful constexpr error message when array
                    size exceeds 16 bytes
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ajcozzol at us dot ibm.com
  Target Milestone: ---

In C++17 mode, GCC is showing an unhelpful error message when diagnosing an
incorrect constexpr function, but only if the size of the array in use exceeds
16. No error messages show up in C++20 or above.

I saw this initially on 14.3.1 (eabi-unknown ARM), but it is the same with GCC
15.2 (x64).

This may be a duplicate of 110401 and 113595 (I'm not sure), but I thought it
was interesting that the error message is correct for arrays of size 16 or
smaller.

Compiler Explorer link: https://godbolt.org/z/rejbGeP9T

#include <array>
#include <cstddef>
#include <cstdint>

template <std::size_t N>
constexpr std::array<std::byte, N> make_byte_array(
    const std::uint8_t (&a)[N]) {
    // Error on next line of code - uninitialized var in constant context
    std::array<std::byte, N> ret;
    for (std::size_t i = 0; i < N; ++i) ret[i] = std::byte{a[i]};
    return ret;
}

int main() {
    // Correct error message for this line:
    // <source>:9:30: error: uninitialized variable 'ret' in 'constexpr'
context
    // constexpr std::array<std::byte, 16> a = make_byte_array({ 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});

    // Once you go past 16, you get a bad and confusing error message:
    // <source>:10:5: error: 'goto' is not a constant expression
    constexpr std::array<std::byte, 17> a = make_byte_array({0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});

    return 0;
}

Reply via email to