0

What I've got right now is a setup like this:

template<typename T> class Parent {
    struct Nested;
    std::unique_ptr<Nested> ptr;
public:
    // stuff
    ~Parent();
};
template<typename T> struct Parent<T>::Nested {
    Parent<T> p;
    // stuff
};

Visual Studio gives me an error that the Nested struct is using an incomplete type Parent- even though the definition is after the Parent class is complete.

How can I resolve this issue?

Edit: In case it wasn't apparent, I have used a delayed destructor definition until after the Nested class is defined to allow a unique_ptr to an incomplete type. In addition, it most definitely is not related to that, because the error isn't that Nested is incomplete- it's that Nested contains a member of type Parent, which is incomplete. In addition, if I explicitly erase the deleter by using std::function<void(Nodes*)> the problem is not solved.

Puppy
  • 138,897
  • 33
  • 232
  • 446

1 Answers1

1

The problem is more with the unique_ptr and not with the class. Change it to shared_ptr or normal raw pointer and the code works fine.

Please find the below link from Stackoverflow.

Does std::unique_ptr requires to know the full T definition?

UPDATE
As per the above link, the code works with unique_ptr if Parent was not templated.

Community
  • 1
  • 1
Jagannath
  • 3,918
  • 22
  • 28
  • `shared_ptr` and `unique_ptr` provide different types of functionality, if they could be randomly replaced with each other, they wouldn't exist as separate types would they? – Alok Save Mar 22 '12 at 02:43
  • Yes, I agree. But, this problem arises when you would want to use unique_ptr for a type that is does not have full definition yet. – Jagannath Mar 22 '12 at 02:46
  • :-) Just saying. It's ironical that the person who asked this question bagged 2 points for an answer I provided in the link. http://stackoverflow.com/a/6088400/124797 – Jagannath Mar 22 '12 at 02:49
  • The first paragraph is wrong, the link provides a good answer to a related question (I bought it and vote to close as duplicate, it is not), and the update does not explain why the template should matter... – David Rodríguez - dribeas Mar 22 '12 at 03:24
  • Except the technique of delaying the destructor is well-known and applied here. And secondly, if you look at the error, it's *not* that `Nested` is an incomplete type. It's that `Nested` contains a `Parent`, which is apparently incomplete. – Puppy Mar 22 '12 at 04:03