Using GetAllActorsOfClass() with reinterpret_cast is safe?

UGameplayStatics::GetAllActorsOfClass() is very useful function but it’s argument type is TArray<AActor*>&. It often should be annoying, so I wrote like this:

TArray<AMyClass*> OutActors;
UGameplayStatics::GetAllActorsOfClass(this, AMyClass::StaticClass(), *(reinterpret_cast<TArray<AActor*>*>(&OutActors)));

It works fine, but I cannot sure it’s memory safety. Is it safe?

Hi. Casting TArray where T is a pointer type should work nice on x86\x64 architectures and most other. It works relying on that fact that all pointers have equal structure, size and constructor\destructor. According to standard it’s an undefined behaviour and once something may go a wrong way. In current implementation all of TArray<*> templates generates the same code. But there is a pitfall if you decide once to cast TArray pointer type to any of it’s non-primary parent (primary parent meant the first parent of a class, it’s base address within class equals to the class base address itself) because in this case these two TArrays have a different structure. So, if your classes have only one parent it’s possible to use this hack.