UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
TGuardedInt< IntType > Class Template Reference

#include <GuardedInt.h>

Public Member Functions

 TGuardedInt ()=default
 
template<UE::CIntegral ValueType>
 TGuardedInt (ValueType InValue)
 
 TGuardedInt (const TGuardedInt &Other)=default
 
TGuardedIntoperator= (const TGuardedInt &Other)=default
 
bool IsValid () const
 
IntType Get (const IntType DefaultValue) const
 
IntType GetChecked (const IntType DefaultValue=0) const
 
bool operator== (const TGuardedInt Other) const
 
bool operator!= (const TGuardedInt Other) const
 
bool ComparisonValid (const TGuardedInt Other) const
 
template<typename ValueType >
bool ValidAndLessThan (const ValueType Other) const
 
template<typename ValueType >
bool ValidAndLessOrEqual (const ValueType Other) const
 
template<typename ValueType >
bool ValidAndGreaterThan (const ValueType Other) const
 
template<typename ValueType >
bool ValidAndGreaterOrEqual (const ValueType Other) const
 
template<typename ValueType >
bool InvalidOrLessThan (const ValueType Other) const
 
template<typename ValueType >
bool InvalidOrLessOrEqual (const ValueType Other) const
 
template<typename ValueType >
bool InvalidOrGreaterThan (const ValueType Other) const
 
template<typename ValueType >
bool InvalidOrGreaterOrEqual (const ValueType Other) const
 
TGuardedInt operator- () const
 
TGuardedInt operator+ (const TGuardedInt Other) const
 
TGuardedInt operator- (const TGuardedInt Other) const
 
TGuardedInt operator* (const TGuardedInt Other) const
 
TGuardedInt operator/ (const TGuardedInt Other) const
 
TGuardedInt operator% (const TGuardedInt Other) const
 
TGuardedInt operator<< (const TGuardedInt Other) const
 
TGuardedInt operator>> (const TGuardedInt Other) const
 
TGuardedInt Abs () const
 

Detailed Description

template<UE::CIntegral IntType>
class TGuardedInt< IntType >

Overflow- and error-checked integer. For integer arithmetic on data from untrusted sources (like imported files), especially when doing size computations. Also checks for division by zero and invalid shift amounts.

You're not meant to use this directly. Use FGuardedInt32 or FGuardedInt64 (defined below). A typical use case would be:

FGuardedInt64 NumBytes = FGuardedInt32(Width) * Height * BytesPerPixel; if (NumBytes.InvalidOrGreaterThan(SizeLimit)) { // Report error. } int64 NumBytesValidated = NumBytes.GetValue();

This is a template meant to be instantiated on top of regular basic integer types. The code is written so the logic is integer-size agnostic and uses just regular C++ arithmetic operations. It is assumed to run on a two's complement integer platform (which is all we support, and as of C++20 is contractual). You should generally use the specializations FGuardedInt32 and FGuardedInt64 below.

Checked integers keep both the integer value and a "valid" flag. Default-constructed guarded ints are invalid, and guarded integers constructed from an integer value are valid and hold that value. Guarded integers are somewhat analogous to a TOptional<IntType> in semantics, and borrow some of the function names.

The main feature of guarded integers is that all arithmetic on them is overflow-checked. Any arithmetic involving guarded integers results in a guarded integer, and any arithmetic involving invalid values, or arithmetic resulting in overflows or other errors (such as division by zero) likewise results in an invalid value. The idea is that integer arithmetic using guarded integers should be possible to write very straightforwardly and without having to consider any of these special cases; if any error occurred along the way, the result will be invalid. These invalid values can then be checked for and handled right when the result is converted back to a regular integer.

Some compilers provide built-ins for overflow-checked integer arithmetic for some types. We could eventually use this (it's especially interesting for multiplications, since our current overflow-checking algorithm is fairly expensive), but a big benefit of the current approach is that it uses nothing but regular arithmetic and is type-agnostic. In particular, this makes it possible to check this implementation exhaustively against a known-good reference for small integer types such as int8. It is much trickier and more subtle to do good testing for larger integer types where that brute-force approach is not practical. As-is, the current approach is not the fastest, but it's not presently intended to be used in contexts where speed of arithmetic operations is a major concern.

Constructor & Destructor Documentation

◆ TGuardedInt() [1/3]

template<UE::CIntegral IntType>
TGuardedInt< IntType >::TGuardedInt ( )
default

Construct a TGuardedInt with an invalid value.

◆ TGuardedInt() [2/3]

template<UE::CIntegral IntType>
template<UE::CIntegral ValueType>
TGuardedInt< IntType >::TGuardedInt ( ValueType  InValue)
inlineexplicit

Construct a TGuardedInt from a regular signed integer value. If it's out of range, it results in an invalid value.

◆ TGuardedInt() [3/3]

template<UE::CIntegral IntType>
TGuardedInt< IntType >::TGuardedInt ( const TGuardedInt< IntType > &  Other)
default

Copy-construct a TGuardedInt from another of matching type.

Member Function Documentation

◆ Abs()

template<UE::CIntegral IntType>
TGuardedInt TGuardedInt< IntType >::Abs ( ) const
inline
Returns
The absolute value of *this.

◆ ComparisonValid()

template<UE::CIntegral IntType>
bool TGuardedInt< IntType >::ComparisonValid ( const TGuardedInt< IntType >  Other) const
inline
Returns
true if *this and Other are both valid so they can be compared.

◆ Get()

template<UE::CIntegral IntType>
IntType TGuardedInt< IntType >::Get ( const IntType  DefaultValue) const
inline
Returns
The value if valid, DefaultValue otherwise.

◆ GetChecked()

template<UE::CIntegral IntType>
IntType TGuardedInt< IntType >::GetChecked ( const IntType  DefaultValue = 0) const
inline
Returns
Returns the value if valid, DefaultValue otherwise, but also check()s that the value is valid. Intended for cases where the value is not expected to be invalid.

◆ InvalidOrGreaterOrEqual()

template<UE::CIntegral IntType>
template<typename ValueType >
bool TGuardedInt< IntType >::InvalidOrGreaterOrEqual ( const ValueType  Other) const
inline
Returns
true if either of *this or Other are invalid or *this is greater than or equal to Other.

◆ InvalidOrGreaterThan()

template<UE::CIntegral IntType>
template<typename ValueType >
bool TGuardedInt< IntType >::InvalidOrGreaterThan ( const ValueType  Other) const
inline
Returns
true if either of *this or Other are invalid or *this is greater than Other.

◆ InvalidOrLessOrEqual()

template<UE::CIntegral IntType>
template<typename ValueType >
bool TGuardedInt< IntType >::InvalidOrLessOrEqual ( const ValueType  Other) const
inline
Returns
true if either of *this or Other are invalid or *this is less than or equal to Other.

◆ InvalidOrLessThan()

template<UE::CIntegral IntType>
template<typename ValueType >
bool TGuardedInt< IntType >::InvalidOrLessThan ( const ValueType  Other) const
inline
Returns
true if either of *this or Other are invalid or *this is less than Other.

◆ IsValid()

template<UE::CIntegral IntType>
bool TGuardedInt< IntType >::IsValid ( ) const
inline
Returns
true if current value is valid (assigned and no overflows or other errors occurred), false otherwise.

◆ operator!=()

template<UE::CIntegral IntType>
bool TGuardedInt< IntType >::operator!= ( const TGuardedInt< IntType >  Other) const
inline
Returns
true if *this and Other either have different "valid" states or are both valid and have different values, false otherwise (logical negation of ==).

◆ operator%()

template<UE::CIntegral IntType>
TGuardedInt TGuardedInt< IntType >::operator% ( const TGuardedInt< IntType >  Other) const
inline
Returns
The remainder when dividing *this by Other.

◆ operator*()

template<UE::CIntegral IntType>
TGuardedInt TGuardedInt< IntType >::operator* ( const TGuardedInt< IntType >  Other) const
inline
Returns
The product of the two operands.

◆ operator+()

template<UE::CIntegral IntType>
TGuardedInt TGuardedInt< IntType >::operator+ ( const TGuardedInt< IntType >  Other) const
inline
Returns
The sum of the two operands.

◆ operator-() [1/2]

template<UE::CIntegral IntType>
TGuardedInt TGuardedInt< IntType >::operator- ( ) const
inline
Returns
The negated value.

◆ operator-() [2/2]

template<UE::CIntegral IntType>
TGuardedInt TGuardedInt< IntType >::operator- ( const TGuardedInt< IntType >  Other) const
inline
Returns
The difference between the two operands.

◆ operator/()

template<UE::CIntegral IntType>
TGuardedInt TGuardedInt< IntType >::operator/ ( const TGuardedInt< IntType >  Other) const
inline
Returns
The quotient when dividing *this by Other.

◆ operator<<()

template<UE::CIntegral IntType>
TGuardedInt TGuardedInt< IntType >::operator<< ( const TGuardedInt< IntType >  Other) const
inline
Returns
This value bitwise left-shifted by the operand.

◆ operator=()

template<UE::CIntegral IntType>
TGuardedInt & TGuardedInt< IntType >::operator= ( const TGuardedInt< IntType > &  Other)
default

Assign a TGuardedInt to another.

◆ operator==()

template<UE::CIntegral IntType>
bool TGuardedInt< IntType >::operator== ( const TGuardedInt< IntType >  Other) const
inline
Returns
true if *this and Other are either both invalid or both valid and have the same value, false otherwise.

◆ operator>>()

template<UE::CIntegral IntType>
TGuardedInt TGuardedInt< IntType >::operator>> ( const TGuardedInt< IntType >  Other) const
inline
Returns
This value bitwise right-shifted by the operand.

◆ ValidAndGreaterOrEqual()

template<UE::CIntegral IntType>
template<typename ValueType >
bool TGuardedInt< IntType >::ValidAndGreaterOrEqual ( const ValueType  Other) const
inline
Returns
true if *this and Other are both valid and *this is greater than or equal to Other.

◆ ValidAndGreaterThan()

template<UE::CIntegral IntType>
template<typename ValueType >
bool TGuardedInt< IntType >::ValidAndGreaterThan ( const ValueType  Other) const
inline
Returns
true if *this and Other are both valid and *this is greater than Other.

◆ ValidAndLessOrEqual()

template<UE::CIntegral IntType>
template<typename ValueType >
bool TGuardedInt< IntType >::ValidAndLessOrEqual ( const ValueType  Other) const
inline
Returns
true if *this and Other are both valid and *this is less than or equal to Other.

◆ ValidAndLessThan()

template<UE::CIntegral IntType>
template<typename ValueType >
bool TGuardedInt< IntType >::ValidAndLessThan ( const ValueType  Other) const
inline
Returns
true if *this and Other are both valid and *this is less than Other.

The documentation for this class was generated from the following file: