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

            Bug ID: 70247
           Summary: constexpr error using the address of an initialized
                    reference
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The following is another constexpr problem I noticed while testing a fix for
bug 70170.

According to [expr.const]:

A conditional-expression e is a core constant expression unless the evaluation
of e, following the rules of the abstract machine (1.9), would evaluate one of
the following expressions:
...
-- an id-expression that refers to a variable or data member of reference type
unless the reference has a preceding initialization and either
   -- it is initialized with a constant expression or
   -- it is a non-static data member of an object whose lifetime began within
the evaluation of e;

the following program is well-formed but it's rejected by all versions of GCC
that support constexpr.  Clang accepts it with a -Wtautological-pointer-compare
warning.

$ /build/gcc-trunk-bootstrap/gcc/xgcc -B /build/gcc-trunk-bootstrap/gcc -S
-Wall -Wextra -Wpedantic -xc++ t.c
t.c:2:27: warning: the address of ‘i’ will never be NULL [-Waddress]
 constexpr bool b0 = &i == 0;
                           ^
t.c:5:24: warning: the compiler can assume that the address of ‘r’ will never
be NULL [-Waddress]
 constexpr bool b1 = &r == 0;
                     ~~~^~~~
t.c:5:27: error: the value of ‘r’ is not usable in a constant expression
 constexpr bool b1 = &r == 0;
                           ^
t.c:4:12: note: ‘r’ was not declared ‘constexpr’
 const int &r = 0;
            ^

Reply via email to