Source: int main() { { constexpr int array[1] = {0}; constexpr int i = array[0]; // OK. } { constexpr int array[1] = {}; constexpr int i = array[0]; // error: array subscript out of bound. Why? } } Compile errors: a.cpp: In function 'int main()': a.cpp:5:17: warning: unused variable 'i' [-Wunused-variable] a.cpp:9:28: error: array subscript out of bound a.cpp:9:17: warning: unused variable 'i' [-Wunused-variable] Access to array[0] is valid. But, compiler says "that's an error."
Confirmed mainline and 4_6-branch.
Better testcase: // PR c++/48911 // { dg-do compile } // { dg-options "-std=c++0x" } struct A { constexpr A () : a (6) {} int a; }; int main () { constexpr int a[1] = { 0 }; constexpr int i = a[0]; constexpr int b[1] = { }; constexpr int j = b[0]; constexpr char c[2] = "a"; constexpr char k = c[1]; constexpr char d[2] = ""; constexpr char l = d[1]; constexpr wchar_t e[2] = L"a"; constexpr wchar_t m = e[1]; constexpr wchar_t f[2] = L""; constexpr wchar_t n = f[1]; constexpr A g[2] = { A () }; constexpr A o = g[0]; constexpr A p = g[1]; } which covers also the case where ary is not CONSTRUCTOR, but STRING_CST. I think we want to to the len check as done right now, but if index is above len, we shouldn't error or set non-constant unconditionally, instead we should check domain of TREE_TYPE (oldary). If it is within the range, we want to return zero of the appropriate type for PODs, not sure what exactly for constexpr non-PODs.
Author: jason Date: Fri May 6 21:57:41 2011 New Revision: 173510 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173510 Log: PR c++/48911 * semantics.c (cxx_eval_array_reference): Handle implicit initializers. Added: trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-missing.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/semantics.c trunk/gcc/testsuite/ChangeLog
Author: jason Date: Fri May 6 21:58:37 2011 New Revision: 173515 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=173515 Log: PR c++/48911 * semantics.c (cxx_eval_array_reference): Handle implicit initializers. Added: branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/constexpr-missing.C Modified: branches/gcc-4_6-branch/gcc/cp/ChangeLog branches/gcc-4_6-branch/gcc/cp/semantics.c branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Fixed for 4.6.1.