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

            Bug ID: 71448
           Summary: pointer relational comparison fails inside constant
                    expression
           Product: gcc
           Version: 5.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at adamsimpkins dot net
  Target Milestone: ---

Created attachment 38656
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=38656&action=edit
Simple repro code

Comparing two pointers with less than/greater than fails inside a constant
expression when compiling without optimization enabled.  Failing code:

    static constexpr const char foo[] = "foo";
    static constexpr const char* bar = "bar";

    static_assert((foo + 3 - foo) == 3, "check");  // Works
    static_assert(foo + 2 != foo, "check");        // Works
    static_assert(foo + 2 >= foo, "check");        // Works
    static_assert(foo + 3 >= foo, "check");        // Works

    static_assert((bar + 3 - bar) == 3, "check");  // Works
    static_assert(bar + 2 != bar, "check");        // Works
    static_assert(bar + 2 >= bar, "check");        // Compile error without -O2
    static_assert(bar + 3 >= bar, "check");        // Compile error without -O2

This seems very similar to bug #67376, but not quite the same.
As best I can tell from reading the standard, this pointer arithmetic is
defined, so this seems like it should be a valid constant expression.

When compiling with -O2 or -O3 the code compiles correctly, and static_assert
checks pass/fail as expected.

I've confirmed this behavior with gcc 4.9 (built from r221427) and gcc 5.3
(built from r233903).

Reply via email to