Is it possible to forward declare TUniquePtr?

Hi,

This compiles fine using TSharedPtr
image

in CPP
image

However changing to TUniquePtr doesn’t compile
image

CPP
image

image

I understand I have to include the header file where that struct exists but is there no way to forward declare with uniquePtr while it’s possible with shared ptr ?
PS: I have the header file included in the CPP I just don’t want to have it included in the header file.

Thank you.

2 Likes

Yes, it is possible;

In UniquePtr.h you will find the following comment:

// If you get an error here when trying to use a TUniquePtr<FForwardDeclaredType> inside a UObject then:
//
// * Declare all your UObject's constructors and destructor in the .h file.
// * Define all of them in the .cpp file.  You can use UMyObject::UMyObject() = default; to auto-generate
//   the default constructor and destructor so that they don't have to be manually maintained.
// * Define a UMyObject(FVTableHelper& Helper) constructor too, otherwise it will be defined in the
//   .gen.cpp file where your pimpl type doesn't exist.  It cannot be defaulted, but it need not
//   contain any particular implementation; the object just needs to be garbage collectable.
//
// If this is efficiency is less important than simplicity, you may want to consider
// using a TPimplPtr instead, though this is also pretty efficient too.

Essentially, you need to have your constructor and destructor declared in your .h and implemented in your .cpp, and if you rely on a.generated.h you also need the constructor with FVTableHelper& Helper.

When you don’t, then the compiler still generates these constructors/destructors in the header file for you, and the destructor will refer to the constructors and destructor of TUniquePtr which needs to know about the constructors and destructor of your FRepDataDudu type - causing the compilation error if you only forward declared your type.

1 Like

Thank you for the solution I appreciate it.

1 Like