Placement new can modify const-qualified variables (original) (raw)

Consider:

namespace std { using size_t = decltype(sizeof(0)); }

void operator new(std::size_t, void p) { return p; } void operator new[] (std::size_t, void p) {return p;}

constexpr int foo() { const int a = 10;

new ((int*)&a) int(12);

return a; } static_assert(foo() == 12);

https://godbolt.org/z/7jj8YsE8r

This should not be accepted of course.

This might be a good first issue, I'm not sure. Maybe @cor3ntin can comment on that.