mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 06:48:31 +00:00
Fix bugs in in ctl::optional (#1203)
Manually manage the lifetime of `value_` by using an anonymous `union`. This fixes a bunch of double-frees and double-constructs. Additionally move the `present_` flag last. When `T` has padding `present_` will be placed there saving `alignof(T)` bytes from `sizeof(optional<T>)`.
This commit is contained in:
parent
2ba6b0158f
commit
d44a7dc603
2 changed files with 32 additions and 12 deletions
|
@ -26,6 +26,8 @@
|
|||
// #include <string>
|
||||
// #define ctl std
|
||||
|
||||
static int g = 0;
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
|
@ -114,6 +116,13 @@ main()
|
|||
return 24;
|
||||
}
|
||||
|
||||
{
|
||||
struct A { int* p = &g; A() {++*p; } };
|
||||
ctl::optional<A> x;
|
||||
if (g != 0)
|
||||
return 25;
|
||||
}
|
||||
|
||||
CheckForMemoryLeaks();
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue