Bug 86623 - constexpr evaluation fails to give an error for modifying a const object
Summary: constexpr evaluation fails to give an error for modifying a const object
Status: RESOLVED DUPLICATE of bug 85125
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2018-07-21 20:50 UTC by David Stone
Modified: 2019-06-22 01:57 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-08-14 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Stone 2018-07-21 20:50:06 UTC
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());
Comment 1 2018-09-11 17:17:50 UTC
This looks like a duplicate of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85125
Comment 2 David Stone 2019-06-22 01:57:04 UTC
Closing as duplicate

*** This bug has been marked as a duplicate of bug 85125 ***