[#absl-NoDestructor-08]
= xref:absl.adoc[absl]::NoDestructor
:relfileprefix: ../
:mrdocs:

NoDestructor&lt;T&gt; is a wrapper around an object of type T that behaves as an object of type T but never calls T&apos;s destructor&period; NoDestructor&lt;T&gt; makes it safer and&sol;or more efficient to use such objects in static storage contexts, ideally as function scope static variables&period;

== Synopsis

Declared in `&lt;absl&sol;base&sol;no&lowbar;destructor&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
template&lt;typename T&gt;
class NoDestructor;
----

== Description

An instance of absl&colon;&colon;NoDestructor&lt;T&gt; has similar type semantics to an instance of T&colon;

&ast; Constructs in the same manner as an object of type T through perfect forwarding&period; &ast; Provides pointer&sol;reference semantic access to the object of type T via `&hyphen;&gt;`, `&ast;`, and `get()`&period; (Note that `const NoDestructor&lt;T&gt;` works like a pointer to const `T`&period;)

Additionally, NoDestructor&lt;T&gt; provides the following benefits&colon;

&ast; Never calls T&apos;s destructor for the object &ast; If the object is a function&hyphen;local static variable, the type can be lazily constructed&period;

An object of type NoDestructor&lt;T&gt; is &quot;trivially destructible&quot; in the notion that its destructor is never run&period;

Usage as Function Scope Static Variables

Function static objects will be lazily initialized within static storage&colon;

&sol;&sol; Function scope&period; const std&colon;&colon;string&amp; MyString() &lcub; static const absl&colon;&colon;NoDestructor&lt;std&colon;&colon;string&gt; x(&quot;foo&quot;)&semi; return &ast;x&semi; &rcub;

For function static variables, NoDestructor avoids heap allocation and can be inlined in static storage, resulting in exactly&hyphen;once, thread&hyphen;safe construction of an object, and very fast access thereafter (the cost is a few extra cycles)&period;

Using NoDestructor&lt;T&gt; in this manner is generally better than other patterns which require pointer chasing&colon;

&sol;&sol; Prefer using absl&colon;&colon;NoDestructor&lt;T&gt; instead for the static variable&period; const std&colon;&colon;string&amp; MyString() &lcub; static const std&colon;&colon;string&ast; x &equals; new std&colon;&colon;string(&quot;foo&quot;)&semi; return &ast;x&semi; &rcub;

Usage as Global Static Variables

NoDestructor&lt;T&gt; allows declaration of a global object of type T that has a non&hyphen;trivial destructor since its destructor is never run&period; However, such objects still need to worry about initialization order, so such use is not recommended, strongly discouraged by the Google C&plus;&plus; Style Guide, and outright banned in Chromium&period; See https&colon;&sol;&sol;google&period;github&period;io&sol;styleguide&sol;cppguide&period;html&num;Static&lowbar;and&lowbar;Global&lowbar;Variables

&sol;&sol; Global or namespace scope&period; absl&colon;&colon;NoDestructor&lt;MyRegistry&gt; reg&lcub;&quot;foo&quot;, &quot;bar&quot;, 8008&rcub;&semi;

Note that if your object already has a trivial destructor, you don&apos;t need to use NoDestructor&lt;T&gt;&period;

== Member Functions

[cols="1,4"]
|===
| Name| Description
| xref:absl/NoDestructor-08/2constructor-04a.adoc[`NoDestructor`]         [.small]#[constructor]#
| Constructors
| xref:absl/NoDestructor-08/operator_assign.adoc[`operator&equals;`]  [.small]#[deleted]#
| Deleted&colon; `NoDestructor` is not copy&hyphen;assignable&period;
| xref:absl/NoDestructor-08/get-08a76.adoc[`get`] 
| `get` overloads
| xref:absl/NoDestructor-08/operator_star-00.adoc[`operator&ast;`] 
| Dereference operators
| xref:absl/NoDestructor-08/operator_ptr-0b.adoc[`operator&hyphen;&gt;`] 
| Member access operators
|===


== Deduction Guides

[cols="1,4"]
|===
| Name| Description
| xref:absl/NoDestructor-04.adoc[`NoDestructor&lt;T&gt;`] 
| Provide &apos;Class Template Argument Deduction&apos;&colon; the type of NoDestructor&apos;s T will be the same type as the argument passed to NoDestructor&apos;s constructor&period;
|===



[.small]#Created with https://www.mrdocs.com[MrDocs]#
