UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Requires.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include <type_traits>
7
8/*-----------------------------------------------------------------------------
9 Readability macro for a constraint in template definitions, future-proofed
10 for C++ 20 concepts. Usage:
11
12 template <
13 typename T,
14 typename U // note - no trailing comma before the constraint
15 UE_REQUIRES(std::is_integral_v<T> && sizeof(U) <= 4)
16 >
17 void IntegralUpTo32Bit(T Lhs, U Rhs)
18 {
19 }
20
21 When a template is both declared and defined at once, UE_REQUIRES must be used.
22
23 If a forward declaration is needed, UE_REQUIRES must be used to declare the function
24 outside the scope, then UE_REQUIRES_DEFINITION should be used for the constraint on the
25 function declaration.
26
27 If a friend declaration is needed, a forward declaration using UE_REQUIRES must first be
28 specified at the required scope, then UE_REQUIRES_FRIEND should be used for the constraint
29 in the friendship declaration. Usage:
30
31 template <
32 typename T
33 UE_REQUIRES(trait_v<T>)
34 >
35 void Function(T Val);
36
37 class FThing
38 {
39 private:
40 template <
41 typename T
42 UE_REQUIRES_FRIEND(trait_v<T>)
43 >
44 friend void FuncB(T);
45
46 template <typename T>
47 static void Use(T);
48 };
49
50 template <
51 typename T
52 UE_REQUIRES_DEFINITION(trait_v<T>)
53 >
54 void Function(T Val)
55 {
56 Use(Val);
57 }
58
59 UE_REQUIRES_EXPR() wraps the effects of a requires expression, used to test the
60 compilability of an expression based on a deduced template parameter. Usage:
61
62 template <
63 typename LhsType,
64 typename RhsType
65 UE_REQUIRES(
66 std::is_base_of_v<UObject, LhsType> &&
67 std::is_base_of_v<UObject, RhsType> &&
68 UE_REQUIRES_EXPR(std::declval<const LhsType*>() == std::declval<const RhsType*>())
69 )
70 >
71 bool operator==(const TSmartPtr<LhsType>& Lhs, const TSmartPtr<RhsType>& Rhs)
72 {
73 return Lhs.Get() == Rhs.Get();
74 }
75
76 Unlike a C++20 requires expression, UE_REQUIRES_EXPR() can only be used in
77 the body of a UE_REQUIRES() macro - standalone concept checks must still be
78 done via something like a TModels-type concept.
79 -----------------------------------------------------------------------------*/
80#if __cplusplus < 202000 || (defined(__clang__) && ((!PLATFORM_APPLE && !PLATFORM_ANDROID && __clang_major__ == 16) || (PLATFORM_APPLE && __clang_major__ == 15) || (PLATFORM_ANDROID && __clang_major__ == 17)))
81 // Clang 16 (or Apple Clang 15, or Android Clang 17) treats a UE_REQUIRES_FRIEND declaration as a separate function, causing ambiguous overload resolution,
82 // so fall back to the non-concept implementation.
83 //
84 // https://github.com/llvm/llvm-project/issues/60749
85
86 #define UE_REQUIRES(...) , std::enable_if_t<(__VA_ARGS__), int> = 0
87 #define UE_REQUIRES_FRIEND(...) , std::enable_if_t<(__VA_ARGS__), int>
88 #define UE_REQUIRES_DEFINITION(...) , std::enable_if_t<(__VA_ARGS__), int>
89 #define UE_REQUIRES_EXPR(...) (!std::is_same_v<decltype(__VA_ARGS__), long long************>) // This is highly unlikely to be the type of any expression
90#else
91 namespace UE::Core::Private
92 {
93 // Only needed for the UE_REQUIRES macro to work, to allow for a trailing > token after the macro
94 template <bool B>
95 concept BoolIdentityConcept = B;
96 }
97
98 #define UE_REQUIRES(...) > requires (!!(__VA_ARGS__)) && UE::Core::Private::BoolIdentityConcept<true
99 #define UE_REQUIRES_FRIEND(...) UE_REQUIRES(__VA_ARGS__)
100 #define UE_REQUIRES_DEFINITION(...) UE_REQUIRES(__VA_ARGS__)
101 #define UE_REQUIRES_EXPR(...) requires { (__VA_ARGS__); }
102#endif
103
104#define TEMPLATE_REQUIRES(...) UE_DEPRECATED_MACRO(5.5, "TEMPLATE_REQUIRES has been deprecated - please use UE_REQUIRES instead.") typename TEnableIf<__VA_ARGS__, int>::type = 0
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
implementation
Definition PlayInEditorLoadingScope.h:8