If you only need this functionality from C++ and you plan to do this for more than 1 actor class, you can create a templated version of GetAllActorsOfClass. The implementation below is similar to UGameplayStatics::GetAllActorsOfClass but omits the ActorClass==nullptr check since the compiler will make sure ActorClass is valid.
template <typename ActorClass>
void TGetAllActorsOfClass(UWorld* World, TArray<ActorClass*>& OutActors)
for (TActorIterator<ActorClass> It(World); It; ++It)
void Test(UWorld* World)
Won't compile because UObject not a sub-class of AActor.
Note this function cannot be used from Blueprints. If you need to create typed arrays accessible from blueprints, you’ll need to make a blueprint library function for each array type you want to get actors for.