llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27k stars 11.06k forks source link

[C++] Implement constexpr placement new (P2747R2) #97957

Open cor3ntin opened 1 week ago

cor3ntin commented 1 week ago

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2747r2.html

cor3ntin commented 1 week ago

I started work on this

llvmbot commented 1 week ago

@llvm/issue-subscribers-clang-frontend

Author: cor3ntin (cor3ntin)

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2747r2.html
tbaederr commented 1 week ago

I'm not much of a spec person - does this include placing into an array of different type, e.g.:

char B[sizeof(int));
new (B) int{12};

?

Or is it only supposed to work if the types match, e.g.

int a;
new (&a) int{12};
frederick-vs-ja commented 1 week ago

I'm not much of a spec person - does this include placing into an array of different type, e.g.:

char B[sizeof(int));
new (B) int{12};

?

Or is it only supposed to work if the types match, e.g.

int a;
new (&a) int{12};

I believe the former is not yet covered by P2747R2. Construction is still required to be performed on "properly typed" storage in constant evaluation.

cor3ntin commented 6 days ago

The interconvertibility requirement allows things like


struct S {
  int i;
};

S s;
new (&s) int(0);

because the pointer to s can be converted to a pointer to int (ie we have to chase various bases, subobjects and union members, but it is implementable.

(Without that restriction all existing c++ implementations would have to be thrown away afaik)

zygoloid commented 6 days ago

How can we choose a union member if multiple union members have the same type? That case hits angelic nondeterminism, which we can't really support here.

cor3ntin commented 6 days ago

@zygoloid Haha, we were having that conversation at the same moment. Brian started a thread with CWG https://lists.isocpp.org/core/2024/07/16009.php (in part because the complexity introduced by interconvertibility may not be justified in the cases that would be implementable)