diff options
author | Cronet Mainline Eng <cronet-mainline-eng+copybara@google.com> | 2024-05-28 13:59:50 +0900 |
---|---|---|
committer | Motomu Utsumi <motomuman@google.com> | 2024-05-28 14:11:54 +0900 |
commit | 168f7e285114554eb2ac9bc22343cca461355b50 (patch) | |
tree | c65ccc97fb3dc01e329951c1c7c7901aef7b7a2a /base/no_destructor.h | |
parent | 5cfdd35118d5a23349255971e97737e32895ec0f (diff) | |
download | cronet-168f7e285114554eb2ac9bc22343cca461355b50.tar.gz |
Import Cronet version 122.0.6261.43
FolderOrigin-RevId: /tmp/copybara-origin/src
Change-Id: Ifb7b548cde690e10cc102366bc538e744efa902b
Diffstat (limited to 'base/no_destructor.h')
-rw-r--r-- | base/no_destructor.h | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/base/no_destructor.h b/base/no_destructor.h index bedc9dcb9..8a431ba4c 100644 --- a/base/no_destructor.h +++ b/base/no_destructor.h @@ -22,10 +22,16 @@ namespace base { // // ## Caveats // -// - Must only be used as a function-local static variable. Declaring a global -// variable of type `base::NoDestructor<T>` will still generate a global -// constructor; declaring a local or member variable will lead to memory leaks -// or other surprising and undesirable behaviour. +// - Must not be used for locals or fields; by definition, this does not run +// destructors, and this will likely lead to memory leaks and other +// surprising and undesirable behaviour. +// +// - If `T` is not constexpr constructible, must be a function-local static +// variable, since a global `NoDestructor<T>` will still generate a static +// initializer. +// +// - If `T` is constinit constructible, may be used as a global, but mark the +// global `constinit`. // // - If the data is rarely used, consider creating it on demand rather than // caching it for the lifetime of the program. Though `base::NoDestructor<T>` @@ -76,6 +82,11 @@ namespace base { template <typename T> class NoDestructor { public: + static_assert(!(std::is_trivially_constructible_v<T> && + std::is_trivially_destructible_v<T>), + "T is trivially constructible and destructible; please use a " + "constinit object of type T directly instead"); + static_assert( !std::is_trivially_destructible_v<T>, "T is trivially destructible; please use a function-local static " |