https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92238
Bug ID: 92238
Summary: constexpr fails to compile 2d std::array in C++ 10
master
Product: gcc
Version: 10.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: euloanty at live dot com
Target Milestone: ---
Created attachment 47118
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47118&action=edit
The bugged code
I tried the same code with gcc 9.2, clang, visual C++. They all work. However,
the code fails to compile on gcc master.
It gives me the error message of "error: modifying a const object".
Current directory: D:\git\f3\fast_io\examples\0026.helper
g++ -o gennum gennum.cc -O3 -std=c++2a -fconcepts -Wall -Wextra -ltbb -DNDEBUG
Process started (PID=14596) >>>
gennum.cc: In function 'int main()':
gennum.cc:67:40: in 'constexpr' expansion of 'cal_content<2, false>()'
gennum.cc:67:41: error: modifying a const object
'vals.std::array<std::array<char, 8>, 256>::_M_elems[1].std::array<char,
8>::_M_elems[7]' is not allowed in a constant expression
67 | constexpr auto a(cal_content<2,false>());
| ^
gennum.cc:67:41: note: originally declared 'const' here
gennum.cc:71:19: warning: unused variable 'e' [-Wunused-variable]
71 | for(auto const & e : a)
| ^
<<< Process finished (PID=14596). (Exit code 1)
================ READY ===============
You can check that on Godbolt for different compilers
https://godbolt.org/z/4V4qVk
I think it is because the changes made by Marek Polacek which breaks the code.
It incorrectly detects constructs of objects in constexpr (because of copy
elision rule???)
2019-08-19 Marek Polacek <[email protected]>
PR c++/91264 - detect modifying const objects in constexpr.
* constexpr.c (modifying_const_object_error): New function.
(cxx_eval_call_expression): Set TREE_READONLY on a CONSTRUCTOR of
a const-qualified object after it's been fully constructed.
(modifying_const_object_p): New function.
(cxx_eval_store_expression): Detect modifying a const object
during constant expression evaluation.
(cxx_eval_increment_expression): Use a better location when building
up the store.
(cxx_eval_constant_expression) <case DECL_EXPR>: Mark a constant
object's constructor TREE_READONLY.