https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99418
Martin Sebor <msebor at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |msebor at gcc dot gnu.org
--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
The initialization of a reference binds it to the value of the initializer
expression. In both examples the [value of the] initializer expression is
undefined because it doesn't refer to an element of the array. One way to see
that is in a constexpr context which rejects undefined constructs with an error
(only with Clang; GCC doesn't reject invalid initialization of references
there, see also pr70151):
$ cat t.C && clang -S t.C
constexpr int a[2] = { 1, 2 };
constexpr const int &r = a[2];
t.C:2:26: warning: array index 2 is past the end of the array (which contains 2
elements) [-Warray-bounds]
constexpr const int &r = a[2];
^ ~
t.C:1:1: note: array 'a' declared here
constexpr int a[2] = { 1, 2 };
^
t.C:2:22: error: constexpr variable 'r' must be initialized by a constant
expression
constexpr const int &r = a[2];
^ ~~~~
t.C:2:22: note: dereferenced pointer past the end of subobject of 'a' is not a
constant expression
t.C:1:15: note: declared here
constexpr int a[2] = { 1, 2 };
^
1 warning and 1 error generated.