Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::unique_ptr structure member to the structure type

Tags:

c++

unique-ptr

Is

struct A
{
    std::unique_ptr<A> a;
};

allowed by the standard? I don't think it is for container types like std::set but is there something special about unique_ptr?

like image 373
P45 Imminent Avatar asked Sep 28 '22 08:09

P45 Imminent


1 Answers

Yes, it's explicitly allowed. C++14 (n4140) 20.8.1/5:

... The template parameter T of unique_ptr may be an incomplete type.

It is also allowed for std::shared_ptr and std::weak_ptr, using similar wording.

like image 174
Angew is no longer proud of SO Avatar answered Nov 15 '22 10:11

Angew is no longer proud of SO