Skip to content

Placement new can modify const-qualified variables #131432

@tbaederr

Description

@tbaederr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluationgood first issuehttps://github.com/llvm/llvm-project/contribute

    Type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions