UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
StaticArray.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6// Deprecated in 5.7. Only needed for backward compatibility.
13#include "Templates/TypeHash.h"
14
15namespace UE::Core::Private
16{
17 // This is a workaround for a parsing error in MSVC under /persmissive- builds, which would
18 // get confused by the fold expression in the constraint in the constructor.
19 template <typename InElementType, typename... ArgTypes>
20 constexpr bool TCanBeConvertedToFromAll_V = (std::is_convertible_v<ArgTypes, InElementType> && ...);
21}
22
24template <typename InElementType, uint32 NumElements, uint32 Alignment = uint32(-1)>
26{
27 // Deprecated in 5.7. Friend only needed for backward compatibility.
28 template <typename, uint32, uint32> friend class TStaticArray;
29
30public:
32
33 InElementType Elements[NumElements];
34
35 [[nodiscard]] constexpr TStaticArray() = default;
36
37 // Constructs each element with Args
38 template <typename... ArgTypes>
39 [[nodiscard]] constexpr explicit TStaticArray(EInPlace, ArgTypes&&... Args)
40 : TStaticArray{InPlace, TMakeIntegerSequence<uint32, NumElements>(), [&Args...](uint32) { return InElementType(Args...); }}
41 {
42 }
43
44 // Directly initializes the array with the provided values.
45 template <typename... ArgTypes> requires((sizeof...(ArgTypes) > 0 && sizeof...(ArgTypes) <= NumElements) && UE::Core::Private::TCanBeConvertedToFromAll_V<InElementType, ArgTypes...>)
46 [[nodiscard]] constexpr TStaticArray(ArgTypes&&... Args)
47 : TStaticArray{PerElement, [&Args] { return InElementType(Forward<ArgTypes>(Args)); }...}
48 {
49 }
50
51 [[nodiscard]] constexpr TStaticArray(TStaticArray&& Other) = default;
52 [[nodiscard]] constexpr TStaticArray(const TStaticArray& Other) = default;
53 constexpr TStaticArray& operator=(TStaticArray&& Other) = default;
54 constexpr TStaticArray& operator=(const TStaticArray& Other) = default;
55
56 // Accessors.
58 {
59 checkSlow(Index < NumElements);
60 return Elements[Index];
61 }
62
63 [[nodiscard]] constexpr const InElementType& operator[](uint32 Index) const
64 {
65 checkSlow(Index < NumElements);
66 return Elements[Index];
67 }
68
69 [[nodiscard]] bool operator==(const TStaticArray&) const = default;
70
77 [[nodiscard]] constexpr bool IsEmpty() const
78 {
79 return NumElements == 0;
80 }
81
83 [[nodiscard]] UE_REWRITE constexpr int32 Num() const
84 {
85 return NumElements;
86 }
87
90 {
91 return Elements;
92 }
93
94 [[nodiscard]] UE_REWRITE constexpr const InElementType* GetData() const
95 {
96 return Elements;
97 }
98
99 [[nodiscard]] UE_REWRITE constexpr InElementType* begin () { return Elements; }
100 [[nodiscard]] UE_REWRITE constexpr const InElementType* begin () const { return Elements; }
101 [[nodiscard]] UE_REWRITE constexpr InElementType* end () { return Elements + NumElements; }
102 [[nodiscard]] UE_REWRITE constexpr const InElementType* end () const { return Elements + NumElements; }
107
108private:
109 template <uint32... Indices, typename ArgGeneratorType>
111 : Elements{Generator(Indices)...}
112 {
113 }
114
115 template <typename... ArgGeneratorTypes>
116 constexpr explicit TStaticArray(EPerElement, ArgGeneratorTypes... Generator)
117 : Elements{Generator()...}
118 {
119 }
120};
121
122template <typename InElementType, uint32 NumElements, uint32 Alignment> requires(Alignment != uint32(-1))
124{
125 UE_STATIC_ASSERT_WARN(false, "TStaticArray's alignment parameter has been deprecated in 5.7 - you can use TAlignedElement to wrap InElementType.");
126
128
129public:
131
132 [[nodiscard]] constexpr TStaticArray() = default;
133
134 // Constructs each element with Args
135 template <typename... ArgTypes>
136 [[nodiscard]] constexpr explicit TStaticArray(EInPlace, ArgTypes&&... Args)
137 : TStaticArray<UE::Core::TAlignedElement<InElementType, Alignment>, NumElements>{InPlace, TMakeIntegerSequence<uint32, NumElements>(), [&Args...](uint32) { return InElementType(Args...); }}
138 {
139 }
140
141 // Directly initializes the array with the provided values.
142 template <typename... ArgTypes> requires((sizeof...(ArgTypes) > 0 && sizeof...(ArgTypes) <= NumElements) && UE::Core::Private::TCanBeConvertedToFromAll_V<InElementType, ArgTypes...>)
143 [[nodiscard]] constexpr TStaticArray(ArgTypes&&... Args)
144 : TStaticArray<UE::Core::TAlignedElement<InElementType, Alignment>, NumElements>{PerElement, [&Args] { return InElementType(Forward<ArgTypes>(Args)); }...}
145 {
146 }
147
148 [[nodiscard]] constexpr TStaticArray(TStaticArray&& Other) = default;
149 [[nodiscard]] constexpr TStaticArray(const TStaticArray& Other) = default;
150 constexpr TStaticArray& operator=(TStaticArray&& Other) = default;
151 constexpr TStaticArray& operator=(const TStaticArray& Other) = default;
152
158
164
165 // Accessors.
167 {
168 checkSlow(Index < NumElements);
169 return Elements[Index];
170 }
171
172 [[nodiscard]] inline constexpr const InElementType& operator[](uint32 Index) const
173 {
174 checkSlow(Index < NumElements);
175 return Elements[Index];
176 }
177
178 template <typename StorageElementType, bool bReverse = false>
179 struct FRangedForIterator
180 {
182 : Ptr(InPtr)
183 {
184 }
185
186 [[nodiscard]] constexpr std::conditional_t<std::is_const_v<StorageElementType>, const InElementType, InElementType>& operator*() const
187 {
188 if constexpr (bReverse)
189 {
190 return *(Ptr - 1);
191 }
192 else
193 {
194 return *Ptr;
195 }
196 }
197
198 constexpr FRangedForIterator& operator++()
199 {
200 if constexpr (bReverse)
201 {
202 --Ptr;
203 }
204 else
205 {
206 ++Ptr;
207 }
208 return *this;
209 }
210
211 [[nodiscard]] constexpr bool operator!=(const FRangedForIterator& B) const
212 {
213 return Ptr != B.Ptr;
214 }
215
216 private:
218 };
219
224
228 [[nodiscard]] UE_FORCEINLINE_HINT RangedForIteratorType constexpr end() { return RangedForIteratorType(Elements + NumElements); }
229 [[nodiscard]] UE_FORCEINLINE_HINT RangedForConstIteratorType constexpr end() const { return RangedForConstIteratorType(Elements + NumElements); }
234};
235
237template <typename InElementType, uint32 NumElements>
239{
241 for(uint32 ElementIndex = 0;ElementIndex < NumElements;++ElementIndex)
242 {
243 Result[ElementIndex] = InValue;
244 }
245 return Result;
246}
247
248template <typename ElementType, uint32 NumElements, uint32 Alignment>
249struct TIsContiguousContainer<TStaticArray<ElementType, NumElements, Alignment>>
250{
251 enum { Value = Alignment == uint32(-1) };
252};
253
255template <typename ElementType, uint32 NumElements, uint32 Alignment>
257{
258 for(uint32 Index = 0;Index < NumElements;++Index)
259 {
260 Ar << StaticArray[Index];
261 }
262 return Ar;
263}
264
266template <typename ElementType, uint32 NumElements, uint32 Alignment>
268{
269 uint32 Hash = 0;
270 for (const ElementType& Element : Array)
271 {
273 }
274 return Hash;
275}
#define checkSlow(expr)
Definition AssertionMacros.h:332
EPerElement
Definition CoreMiscDefines.h:163
@ PerElement
Definition CoreMiscDefines.h:163
#define UE_STATIC_ASSERT_WARN(bExpression, Message)
Definition CoreMiscDefines.h:431
EInPlace
Definition CoreMiscDefines.h:162
@ InPlace
Definition CoreMiscDefines.h:162
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
#define UE_REWRITE
Definition Platform.h:747
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
typename UE4IntegerSequence_Private::TMakeIntegerSequenceImpl< T, N >::Type TMakeIntegerSequence
Definition IntegerSequence.h:31
constexpr TStaticArray< InElementType, NumElements > MakeUniformStaticArray(typename TCallTraits< InElementType >::ParamType InValue)
Definition StaticArray.h:238
FArchive & operator<<(FArchive &Ar, TStaticArray< ElementType, NumElements, Alignment > &StaticArray)
Definition StaticArray.h:256
uint32 GetTypeHash(const TStaticArray< ElementType, NumElements, Alignment > &Array)
Definition StaticArray.h:267
constexpr uint32 HashCombineFast(uint32 A, uint32 B)
Definition TypeHash.h:74
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Core.Build.cs:8
Definition Archive.h:1208
constexpr TStaticArray & operator=(const TStaticArray &Other)=default
FRangedForIterator< UE::Core::TAlignedElement< InElementType, Alignment >, true > RangedForReverseIteratorType
Definition StaticArray.h:222
UE_FORCEINLINE_HINT RangedForReverseIteratorType constexpr rbegin()
Definition StaticArray.h:230
constexpr TStaticArray(TStaticArray &&Other)=default
constexpr TStaticArray & operator=(TStaticArray< UE::Core::TAlignedElement< InElementType, Alignment >, NumElements > &&Other)
Definition StaticArray.h:153
FRangedForIterator< const UE::Core::TAlignedElement< InElementType, Alignment >, true > RangedForConstReverseIteratorType
Definition StaticArray.h:223
UE_FORCEINLINE_HINT RangedForConstIteratorType constexpr begin() const
Definition StaticArray.h:227
constexpr const InElementType & operator[](uint32 Index) const
Definition StaticArray.h:172
constexpr TStaticArray & operator=(TStaticArray &&Other)=default
UE_FORCEINLINE_HINT RangedForReverseIteratorType constexpr rend()
Definition StaticArray.h:232
UE_FORCEINLINE_HINT RangedForConstReverseIteratorType constexpr rbegin() const
Definition StaticArray.h:231
UE_FORCEINLINE_HINT RangedForIteratorType constexpr end()
Definition StaticArray.h:228
UE_FORCEINLINE_HINT RangedForConstReverseIteratorType constexpr rend() const
Definition StaticArray.h:233
constexpr TStaticArray(ArgTypes &&... Args)
Definition StaticArray.h:143
constexpr TStaticArray(EInPlace, ArgTypes &&... Args)
Definition StaticArray.h:136
constexpr InElementType & operator[](uint32 Index)
Definition StaticArray.h:166
UE_FORCEINLINE_HINT RangedForIteratorType constexpr begin()
Definition StaticArray.h:226
constexpr TStaticArray & operator=(const TStaticArray< UE::Core::TAlignedElement< InElementType, Alignment >, NumElements > &Other)
Definition StaticArray.h:159
FRangedForIterator< UE::Core::TAlignedElement< InElementType, Alignment > > RangedForIteratorType
Definition StaticArray.h:220
InElementType ElementType
Definition StaticArray.h:130
constexpr TStaticArray(const TStaticArray &Other)=default
FRangedForIterator< const UE::Core::TAlignedElement< InElementType, Alignment > > RangedForConstIteratorType
Definition StaticArray.h:221
UE_FORCEINLINE_HINT RangedForConstIteratorType constexpr end() const
Definition StaticArray.h:229
Definition StaticArray.h:26
constexpr TStaticArray & operator=(const TStaticArray &Other)=default
UE_REWRITE constexpr InElementType * end()
Definition StaticArray.h:101
UE_REWRITE constexpr InElementType * begin()
Definition StaticArray.h:99
friend class TStaticArray
Definition StaticArray.h:28
UE_REWRITE constexpr InElementType * GetData()
Definition StaticArray.h:89
UE_REWRITE constexpr TReversePointerIterator< InElementType > rbegin()
Definition StaticArray.h:103
constexpr TStaticArray(TStaticArray &&Other)=default
constexpr const InElementType & operator[](uint32 Index) const
Definition StaticArray.h:63
UE_REWRITE constexpr const InElementType * GetData() const
Definition StaticArray.h:94
constexpr TStaticArray & operator=(TStaticArray &&Other)=default
InElementType Elements[NumElements]
Definition StaticArray.h:33
UE_REWRITE constexpr TReversePointerIterator< const InElementType > rbegin() const
Definition StaticArray.h:104
UE_REWRITE constexpr int32 Num() const
Definition StaticArray.h:83
constexpr TStaticArray(ArgTypes &&... Args)
Definition StaticArray.h:46
constexpr TStaticArray(EInPlace, ArgTypes &&... Args)
Definition StaticArray.h:39
UE_REWRITE constexpr TReversePointerIterator< const InElementType > rend() const
Definition StaticArray.h:106
constexpr InElementType & operator[](uint32 Index)
Definition StaticArray.h:57
UE_REWRITE constexpr TReversePointerIterator< InElementType > rend()
Definition StaticArray.h:105
constexpr TStaticArray()=default
InElementType ElementType
Definition StaticArray.h:31
bool operator==(const TStaticArray &) const =default
UE_REWRITE constexpr const InElementType * end() const
Definition StaticArray.h:102
constexpr TStaticArray(const TStaticArray &Other)=default
constexpr bool IsEmpty() const
Definition StaticArray.h:77
UE_REWRITE constexpr const InElementType * begin() const
Definition StaticArray.h:100
implementation
Definition PlayInEditorLoadingScope.h:8
constexpr bool TCanBeConvertedToFromAll_V
Definition StaticArray.h:20
Definition AdvancedWidgetsModule.cpp:13
U16 Index
Definition radfft.cpp:71
TCallTraitsParamTypeHelper< T, PassByValue >::ParamType ParamType
Definition UnrealTypeTraits.h:275
Definition IntegerSequence.h:9
Definition IsContiguousContainer.h:16
static constexpr bool Value
Definition IsContiguousContainer.h:20
Definition ReverseIterate.h:13
constexpr FRangedForIterator(StorageElementType *InPtr)
Definition StaticArray.h:181
constexpr bool operator!=(const FRangedForIterator &B) const
Definition StaticArray.h:211
constexpr FRangedForIterator & operator++()
Definition StaticArray.h:198
constexpr std::conditional_t< std::is_const_v< StorageElementType >, const InElementType, InElementType > & operator*() const
Definition StaticArray.h:186
Definition AlignedElement.h:13