Bug 123577 - C++17: Unhelpful constexpr error message when array size exceeds 16 bytes
Summary: C++17: Unhelpful constexpr error message when array size exceeds 16 bytes
Status: RESOLVED DUPLICATE of bug 110401
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 15.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid, diagnostic
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2026-01-13 15:55 UTC by Anthony Cozzolino
Modified: 2026-01-13 17:34 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2026-01-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Anthony Cozzolino 2026-01-13 15:55:47 UTC
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;
}
Comment 1 Anthony Cozzolino 2026-01-13 16:08:20 UTC
Note: I'm not sure if leaving ret uninitialized is actually a constexpr violation to begin with, but since that is being enforced in C++17 mode, it would be nice to have a proper error message.
Comment 2 Drea Pinski 2026-01-13 17:25:53 UTC
(In reply to Anthony Cozzolino from comment #1)
> Note: I'm not sure if leaving ret uninitialized is actually a constexpr
> violation to begin with, but since that is being enforced in C++17 mode, it
> would be nice to have a proper error message.

It is in C++17. C++20 has different rules with respect to uninitialized and constexpr.
Comment 3 Drea Pinski 2026-01-13 17:30:29 UTC
On the trunk the reason is removed so it is even worse.
Comment 4 Drea Pinski 2026-01-13 17:33:31 UTC
The fix for the code is simple though:
std::array<std::byte, N> ret{};

That is value initialize ret.

In C++17, uniniitialized variables are supposed to be rejected so this is accepts invalid for <=16 too.  

C++20 allows uninitialized variables to begin with as long as they are fully initialized at the end of the full constexpr expression.
Comment 5 Drea Pinski 2026-01-13 17:34:36 UTC
Oh the goto part is a dup.

*** This bug has been marked as a duplicate of bug 110401 ***