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

Adopt ownership of a raw pointer into a `std&colon;&colon;unique&lowbar;ptr`&period;

== Synopsis

Declared in `&lt;absl&sol;memory&sol;memory&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
template&lt;typename T&gt;
std::unique&lowbar;ptr&lt;T&gt;
WrapUnique(T* ptr);
----

== Description

Adopts ownership from a raw pointer and transfers it to the returned `std&colon;&colon;unique&lowbar;ptr`, whose type is deduced&period; Because of this deduction, _do not_ specify the template type `T` when calling `WrapUnique`&period;

Example&colon; X&ast; NewX(int, int)&semi; auto x &equals; WrapUnique(NewX(1, 2))&semi; &sol;&sol; &apos;x&apos; is std&colon;&colon;unique&lowbar;ptr&lt;X&gt;&period;

Do not call WrapUnique with an explicit type, as in `WrapUnique&lt;X&gt;(NewX(1, 2))`&period; The purpose of WrapUnique is to automatically deduce the pointer type&period; If you wish to make the type explicit, just use `std&colon;&colon;unique&lowbar;ptr` directly&period;

auto x &equals; std&colon;&colon;unique&lowbar;ptr&lt;X&gt;(NewX(1, 2))&semi;

* or &hyphen; std&colon;&colon;unique&lowbar;ptr&lt;X&gt; x(NewX(1, 2))&semi;

While `absl&colon;&colon;WrapUnique` is useful for capturing the output of a raw pointer factory, prefer &apos;std&colon;&colon;make&lowbar;unique&lt;T&gt;(args&period;&period;&period;)&apos; over &apos;absl&colon;&colon;WrapUnique(new T(args&period;&period;&period;))&apos;&period;

auto x &equals; WrapUnique(new X(1, 2))&semi; &sol;&sol; works, but nonideal&period; auto x &equals; make&lowbar;unique&lt;X&gt;(1, 2)&semi; &sol;&sol; safer, standard, avoids raw &apos;new&apos;&period;

Note that `absl&colon;&colon;WrapUnique(p)` is valid only if `delete p` is a valid expression&period; In particular, `absl&colon;&colon;WrapUnique()` cannot wrap pointers to arrays, functions or void, and it must not be used to capture pointers obtained from array&hyphen;new expressions (even though that would compile!)&period;

== Return Value

A `std&colon;&colon;unique&lowbar;ptr` owning `ptr`&period;

== Parameters

[cols="1,4"]
|===
| Name| Description
| *ptr*
| The raw pointer to take ownership of&period;
|===


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