RealDocs

TOptional::Emplace

function Core Since unknown
#include "Misc/Optional.h"
Access: public

Description

Constructs the optional's value in-place from the given arguments, destroying any previously held value first. Returns a reference to the newly constructed value.

Caveats & Gotchas

  • If the constructor called via Emplace leaves the object in an 'unset' intrusive state (i.e. equal to FIntrusiveUnsetOptionalState), a checkf fires in non-shipping builds. Intrusive optional types must not emplace their own unset sentinel.
  • Emplace always destroys the existing value before constructing the new one, even if the type supports assignment. This is intentional to support non-assignable types but means the destructor always runs on replacement.
  • The returned reference is valid only until the next mutation of the TOptional (Reset, Emplace, or assignment). Do not store it persistently.

Signature

template <typename... ArgsType>
OptionalType& Emplace(ArgsType&&... Args)

Parameters

Name Type Description Default
Args ArgsType&&... Arguments forwarded directly to OptionalType's constructor.

Return Type

OptionalType&

Example

In-place construction of a complex value C++
TOptional<FTransform> CachedTransform;

// Construct FTransform directly inside the optional, no temporary:
CachedTransform.Emplace(FRotator(0, 90, 0), FVector(100, 0, 0));

if (CachedTransform.IsSet())
{
    FVector Loc = CachedTransform->GetLocation();
}

Version History

Introduced in: unknown

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.