

`folly::Indestructible`
source link: https://blog.the-pans.com/folly-indestructible/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

folly::Indestructible<T>
is a class template that makes a static
variable well, indestructible. Notice that it's meant for static
variables in the Meyers Singleton pattern. If it's for heap allocated memory, it would just be called memory leak instead of "indestructible".
It boils down to making a destructor of T
not actually destroy the underlying storage. The trick used by folly
is to use union
.
union Storage {
T value;
Storage() : value() {}
~Storage() {}
};
From cppreference https://en.cppreference.com/w/cpp/language/destructor
For both user-defined or implicitly-defined destructors, after the body of the destructor is executed, the compiler calls the destructors for all non-static non-variant members of the class, in reverse order of declaration, then it calls the destructors of all direct non-virtual base classes
Because value
is a variant member of the union, it won't be destructed if Storage s;
is destructed.
Recommend
-
15
Menu Compile `folly` from source on Arch Linux Lu Pan | 14 Sep 2020 | 1 min read
-
14
公众号好像字数有限制,我分成两篇发了。 string 常见的三种实现方式 eager copy COW SSO ...
-
11
folly 的 sorted_vector_set 和 sorted_vector_map 作者: 张志强 ...
-
9
Indestructible Storage in the Cloud with Apache Bookkeeper Apr 28, 2021...
-
10
-
8
This Video Proves The GMC Hummer EV Is Not As Indestructible As It Looks ...
-
4
Ethereum Is Finally Green. Now What?Ethereum has solved its energy problem, but the Merge has unleashed new power dynamics that so far privilege the Global North. January 3, 2023, 2:00pm...
-
4
Jaina GreyGearMar 4, 2023 7:00 AM
-
9
Why The Toyota Hilux Is Considered The Most Indestructible Truck Ever Made ...
-
5
Home ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK