[Bug c++/99700] uninitialized variable accepted as a constant expression in C++ 20

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Apr 15 00:14:19 GMT 2021


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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
            Summary|gcc takes an uninitialized  |uninitialized variable
                   |variable as a constant      |accepted as a constant
                   |expression                  |expression in C++ 20
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-04-15

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
Confirmed with -std=c++20.  In earlier modes GCC rejects the test case as
expected.

$ (set -x && gcc -O2 -S -Wall -std=c++20 pr99700.C && gcc -O2 -S -Wall
-std=c++17 pr99700.C)
+ gcc -O2 -S -Wall -std=c++20 pr99700.C
+ gcc -O2 -S -Wall -std=c++17 pr99700.C
pr99700.C: In constructor ‘constexpr A::A()’:
pr99700.C:2:29: error: member ‘A::c’ must be initialized by mem-initializer in
‘constexpr’ constructor
    2 |   constexpr A() { c[0] = 0; }
      |                             ^
pr99700.C:3:8: note: declared here
    3 |   char c[2];
      |        ^
pr99700.C: At global scope:
pr99700.C:6:13: error: ‘constexpr A::A()’ called in a constant expression
    6 | constexpr A a;
      |             ^
pr99700.C:2:13: note: ‘constexpr A::A()’ declared here
    2 |   constexpr A() { c[0] = 0; }
      |             ^

When reporting bugs please include all the information we ask for here:
https://gcc.gnu.org/bugs/#need.  Links to other sites are not a substitute.


More information about the Gcc-bugs mailing list