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

            Bug ID: 127108
           Summary: Array bound >= 2^31 is incorrectly sign-extended
                    during size_t non-type template parameter deduction
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: my at rhanqtl dot com
  Target Milestone: ---

g++ rejects below program (run in Compiler Explorer:
https://godbolt.org/z/xqc88xnno):

```
using size_t = decltype(sizeof(0));

template <typename T, size_t N>
constexpr size_t f(T (&)[N]) {
    return N;
}

constexpr size_t M = 1ULL << 31;

extern char a[M];

static_assert(f(a) == M);
```

With errors:

```
<source>:12:16: error: no matching function for call to 'f(char [2147483648])'
   12 | static_assert(f(a) == M);
      |               ~^~~
  • there is 1 candidate
    • candidate 1: 'template<class T, long unsigned int N> constexpr size_t f(T
(&)[N])'
      <source>:4:18:
          4 | constexpr size_t f(T (&)[N]) {
            |                  ^
      • template argument deduction/substitution failed:
        • <source>: In substitution of 'template<class T, long unsigned int N>
constexpr size_t f(T (&)[N]) [with T = char; long unsigned int N =
18446744071562067968]':
        • required from here
          <source>:12:16:   
             12 | static_assert(f(a) == M);
                |               ~^~~
        • error: size of array exceeds maximum object size
'9223372036854775807'
          <source>:4:18:
              4 | constexpr size_t f(T (&)[N]) {
                |                  ^
        • error: size of array exceeds maximum object size
'9223372036854775807'
          <source>:4:20:
              4 | constexpr size_t f(T (&)[N]) {
                |                    ^~~~~~~~
```

Note that in the "in substitution ..." line `N` is `18446744071562067968`, i.e.
`0xffffffff80000000`.

clang++ accepts the program.

Reply via email to