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

            Bug ID: 121267
           Summary: Accepts-invalid deduction of array size from
                    brace-enclosed string literal length (initializer
                    list)
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: janschultke at googlemail dot com
  Target Milestone: ---

Consider the following code:

struct T {
  template<int N>
  T(const char(&)[N]) {}
};

struct S {
  S(const char(&)[5]) {}
};

T t({"awoo"}); // error: initializer-string for char array is too long
S s({"awoo"}); // OK


The line highlighted with // error should produce an error (Clang is correct),
but GCC accepts it. In short, https://eel.is/c++draft/temp.deduct.call#1 states
that N should be deduced from the length of the initializer list, which
contains one element here, so N = 1. This then fails because const char(&)[1]
cannot be initialized using {"awoo"}.

https://github.com/cplusplus/CWG/issues/727 CWG seems convinced that the
wording is clear, meaning that GCC is clearly wrong. However, I'm interested in
writing a paper that would make GCC and MSVC correct (there is implementation
divergence: https://godbolt.org/z/TrYh9Pq87). Therefore, it would be helpful if
someone explained by what mechanism GCC accepts this code, and whether it's
desirable to keep that behavior.

Reply via email to