Navigation
Unreal Engine C++ API Reference > Runtime > Core > Templates
References
Module | Core |
Header | /Engine/Source/Runtime/Core/Public/Templates/RetainedRef.h |
Include | #include "Templates/RetainedRef.h" |
Syntax
template<typename T>
struct TRetainedRef
Remarks
TRetainedRef
A helper class which replaces T& as a function parameter when the reference is intended to be retained by the function (e.g. as a class member). The benefit of this class is that it is a compile error to pass an rvalue which might otherwise bind to a const reference, which is dangerous when the reference is retained.
Example:
struct FRaiiType { explicit FRaiiType(const FThing& InThing) : Ref(InThing) { }
void DoSomething() { Ref.Something(); }
FThing& Ref; };
void Test() { FThing Thing(...); FRaiiType Raii1(Thing); // Compiles Raii.DoSomething(); // Fine
FRaiiType Raii2(FThing(...)); // Compiles Raii.DoSomething(); // Illegal - reference has expired! }
But if the constructor is changed to use TRetainedRef then it fixes that problem:
struct FRaiiType { explicit FRaiiType(TRetainedRef
... }
FThing Thing(...); FRaiiType Raii1(Thing); // Compiles Raii.DoSomething(); // Fine
FRaiiType Raii2(FThing(...)); // Compile error! Raii.DoSomething();
Specializations
Constructors
Type | Name | Description | |
---|---|---|---|
![]() |
TRetainedRef
(
T& InRef |
||
![]() |
TRetainedRef
(
const T& InRef |
Can't construct a non-const reference with a const reference and can't retain a rvalue reference. | |
![]() |
TRetainedRef
(
T&& InRef |
||
![]() |
TRetainedRef
(
const T&& InRef |
Functions
Type | Name | Description | |
---|---|---|---|
![]() ![]() |
T & | Get () |
Operators
Type | Name | Description | |
---|---|---|---|
![]() ![]() |
operator T & () |