Navigation
API > API/Runtime > API/Runtime/Core
Description
Projection() is a related function to Invoke(), in that it can be used to invoke an object with a set of arguments. However, it works by transforming something invocable into something callable, i.e. can be called with a normal parenthesized set of arguments.
// These are equivalent: Projection(I)(Args...) Invoke(I, Args...).
... with the same advantages:
// Can accept member function pointers Invoke(&FObjType::Member, Obj); // equivalent to Obj.Member Invoke(&FObjType::Func, Obj, Args...); // equivalent to Obj.Func(Args...) Projection(&FObjType::Member)(Obj); // equivalent to Obj.Member Projection(&FObjType::Func)(Obj, Args...); // equivalent to Obj.Func(Args...)
// Can operate on pointers, including smart pointers Invoke(&FObjType::Member, ObjPtr); // equivalent to ObjPtr->Member Invoke(&FObjType::Func, ObjPtr, Args...); // equivalent to ObjPtr->Func(Args...) Projection(&FObjType::Member)(ObjPtr); // equivalent to ObjPtr->Member Projection(&FObjType::Func)(ObjPtr, Args...); // equivalent to ObjPtr->Func(Args...)
However, Projection() has some additional advantages:
- Projection(I) returns I unchanged if it is already callable, meaning no redundant stepping in and out of many Invoke() calls in the debugger.
- Projection(...) is variadic and can transform a sequence of invocables into a callable that invokes them one by one:
Projection(A, B, C)(Args...) Invoke(C, Invoke(B, Invoke(A, Args...)))
This allows users to pass a sequence of projections to an algorithm that takes a single projection:
struct FInner { FString Name; }; struct FOuter { FInner Inner; };
// Sort array of outers by the names of inner, Algo::SortBy(ArrayOfOuters, Projection(&FOuter::Inner, &FInner::Name));
| Name | Projection |
| Type | function |
| Header File | /Engine/Source/Runtime/Core/Public/Templates/Projection.h |
| Include Path | #include "Templates/Projection.h" |
template<typename Invocable0Type, typename... InvocableTypes>
AUTORTFM_INFER constexpr auto Projection
(
Invocable0Type && Invocable0,
InvocableTypes &&... Invocables
)