How UE_LIFETIMEBOUND macro suppose to work?

Hi,
I found UE_LIFETIMEBOUND macro in some places in UE source code, for example, in the operator[] of TArray. What I’ve found about this in the UE’s source code is only this

/* Wrap lifetimebound annotations to indicate that a function argument must outlive a return value or constructed object */
#ifndef UE_LIFETIMEBOUND
	#define UE_LIFETIMEBOUND
#endif

At line 758 in file …\UE_5.5\Engine\Source\Runtime\Core\Public\HAL\Platform.h

I just want to mention that I know how references works and what will happen when function will return a reference to the position in the TArray data, and that data will later change etc.

My question is, what adding of this macro will change in the standard C++ behavior, when its definition is empty?
Like, for example in this TArray’s function:

FORCEINLINE ElementType& operator[](SizeType Index) UE_LIFETIMEBOUND
	{
		RangeCheck(Index);
		return GetData()[Index];
	}

Is there something that UHT is adding there or modifying the function definition somehow? What kind of magic is happening there?
I tried to find anything in Google or on this forum but I found nothing :frowning:

Hey @N1ghtwish386,

while the default implementation for that macro is empty there are multiple compiler specific defines in the platform specfic headers.
For example in Engine/Source/Runtime/Core/Public/MSVC/MSVCPlatform.h:

#ifdef __has_cpp_attribute
	#if __has_cpp_attribute(msvc::lifetimebound)
		#define UE_LIFETIMEBOUND [[msvc::lifetimebound]]
	#endif
#endif

And in Engine/Source/Runtime/Core/Public/Clang/ClangPlatform.h:

#define UE_LIFETIMEBOUND [[clang::lifetimebound]]

So the actual behavior at runtime will be compiler specific, e.g. resulting in C26815 or C26816 when using MSVC for cases where a reference to something temporary is outliving the temporary.

1 Like

This is the best specific ever answer! Thank you so much! :slight_smile: That is exactly what I wanted to know.