【发布时间】:2019-04-13 05:01:21
【问题描述】:
对于虚幻引擎中的某些USTRUCT structs,定义了类型特征TStructOpsTypeTraits<T>。这些提供了关于struct T 已实现功能的布尔列表(封装在枚举中)。
- 这些特征有什么用?
- 我什么时候应该在我的项目中为我的自定义
USTRUCTs 定义这些特征?
*引擎内的使用示例:
struct TStructOpsTypeTraitsBase2
{
enum
{
WithZeroConstructor = false, // struct can be constructed as a valid object by filling its memory footprint with zeroes.
WithNoInitConstructor = false, // struct has a constructor which takes an EForceInit parameter which will force the constructor to perform initialization, where the default constructor performs 'uninitialization'.
WithNoDestructor = false, // struct will not have its destructor called when it is destroyed.
WithCopy = !TIsPODType<CPPSTRUCT>::Value, // struct can be copied via its copy assignment operator.
// ...
}
}
像这样使用
template<>
struct TStructOpsTypeTraits<FGameplayEffectContextHandle> : public TStructOpsTypeTraitsBase2<FGameplayEffectContextHandle>
{
enum
{
WithCopy = true, // Necessary so that TSharedPtr<FGameplayEffectContext> Data is copied around
WithNetSerializer = true,
WithIdenticalViaEquality = true,
};
};
似乎特征用于蓝图中使用的USTRUCTs;并且它们是具有NetSerialize() 函数的结构所必需的。我做了抽查:
-
WithIdenticalViaEquality->UScriptStruct::HasIdentical()->EStructFlags::STRUCT_IdenticalNative仅在用于蓝图的::IdenticalHelper()中使用 -
EStructFlags::STRUCT_NetSerializeNative用于错误消息(当在蓝图中使用结构时)以及在FObjectReplicator和FRepLayout中,自定义属性复制需要存在此特征 -
TStructOpsTypeTraitsBase2的描述似乎表明,这些特征仅在蓝图中使用USTRUCTs 时才重要类型特征以涵盖脚本结构的自定义方面
UnrealEngine 还为其容器类定义了许多专门的特征(例如TTypeTraitsBase)。与 c++ type_traits 进行比较可能是有意义的。
【问题讨论】:
标签: unreal-engine4