[Bug c++/86623] New: constexpr evaluation fails to give an error for modifying a const object

david at doublewise dot net gcc-bugzilla@gcc.gnu.org
Sat Jul 21 20:51:00 GMT 2018


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

            Bug ID: 86623
           Summary: constexpr evaluation fails to give an error for
                    modifying a const object
           Product: gcc
           Version: 8.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: david at doublewise dot net
  Target Milestone: ---

The following code is compiled by gcc with -std=c++17



constexpr bool f() {
    int const a = 0;
    const_cast<int &>(a) = 2;
    return a == 0;
}

static_assert(f());


This should not be allowed because we modify a const value in a constant
expression.

Here is another example test case that does not use const_cast (and gives a
different answer in the return statement, but I don't think that's especially
relevant because it's undefined behavior anyway):


struct S {
    int a = 1;
    int * ptr = &a;
};

constexpr bool f() {
    auto const s = S{};
    *s.ptr = 2;
    return s.a == 2;
}

static_assert(f());


More information about the Gcc-bugs mailing list