How to declare TArray of TUniquePtr?

Greetings. For Testing I created an empty class A and in GameState declare

private
    TArray<TUniquePtr<A>> Test;

I tried to disable default copy and assignment constructors. Also I’ve tried to add move semantics to my class. But always I get an error:

error C2248:
‘TUniquePtr::TUniquePtr’ : cannot
access private member declared in
class ‘TUniquePtr’

Interestingly, I found such usage in unreal code and there all works without any constructors addition or deletion.

Hi,

Is your TArray inside a UClass? Because CoreUObject is not currently move-aware, so having a move-only member of your class (like a TArray of TUniquePtr is) will make your UClass move-only, and CoreUObject won’t like that.

Steve

Yeah, that’s exactly was a problem - can’t get rid of copy constructor. I just switched for shared pointers. Thank you.

I hit a similar problem with TArray of a class without a copy constructor hosted in a type deriving from TSharedFromThis:

class FMyClass : public TSharedFromThis<FMyClass>
{
   TArray<FTypeWithoutCopyConstructor> MyField;
};

The solution turned out to be explicitly removing the copy constructor from FMyClass:

class FMyClass : public TSharedFromThis<FMyClass>
{
   FMyClass(FMyClass const&) = delete;
   TArray<FTypeWithoutCopyConstructor> MyField;
};

Interestingly, this was only ever an issue in the Development Editor configuration.

1 Like

The assignment operator of the TArray<TUniquePtr<...>> hosting class must also be deleted.

FMyClass& operator=(const FMyClass&) = delete;
1 Like