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

Creates a Cord that takes ownership of external string memory&period;

== Synopsis

Declared in `&lt;absl&sol;strings&sol;cord&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
template&lt;typename Releaser&gt;
xref:absl/Cord.adoc[Cord]
MakeCordFromExternal(
    std::string&lowbar;view data,
    Releaser&& releaser);
----

== Description

The contents of `data` are not copied to the Cord&semi; instead, the external memory is added to the Cord and reference&hyphen;counted&period; This data may not be changed for the life of the Cord, though it may be prepended or appended to&period;

`MakeCordFromExternal()` takes a callable &quot;releaser&quot; that is invoked when the reference count for `data` reaches zero&period; As noted above, this data must remain live until the releaser is invoked&period; The callable releaser also must&colon;

&ast; be move constructible &ast; support `void operator()(absl&colon;&colon;string&lowbar;view)` or `void operator()()`

Example&colon;

Cord MakeCord(BlockPool&ast; pool) &lcub; Block&ast; block &equals; pool&hyphen;&gt;NewBlock()&semi; FillBlock(block)&semi; return absl&colon;&colon;MakeCordFromExternal( block&hyphen;&gt;ToStringView(), &lsqb;pool, block&rsqb;v) &lcub; pool&hyphen;&gt;FreeBlock(block, v)&semi; &rcub;)&semi; &rcub;

WARNING&colon; Because a Cord can be reference&hyphen;counted, it&apos;s likely a bug if your releaser doesn&apos;t do anything&period; For example, consider the following&colon;

void Foo(const char&ast; buffer, int len) &lcub; auto c &equals; absl&colon;&colon;MakeCordFromExternal(absl&colon;&colon;string&lowbar;view(buffer, len),  &lcub;&rcub;)&semi;

&sol;&sol; BUG&colon; If Bar() copies its cord for any reason, including keeping a &sol;&sol; substring of it, the lifetime of buffer might be extended beyond &sol;&sol; when Foo() returns&period; Bar(c)&semi; &rcub;

== Return Value

A Cord referencing the external memory&period;

== Template Parameters

[cols="1,4"]
|===
| Name| Description
| *Releaser*
| The callable type invoked when the data is released&period;
|===

== Parameters

[cols="1,4"]
|===
| Name| Description
| *data*
| The external memory the Cord takes ownership of&period;
| *releaser*
| The callable invoked once the reference count reaches zero&period;
|===


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