xenium icon indicating copy to clipboard operation
xenium copied to clipboard

Support for shared_ptr values

Open benstadin opened this issue 1 year ago • 1 comments

Is there a reason why a value cane be unique_ptr but not shared_ptr? I'm getting the following error when trying to define a ramalhete_queue using shared_ptr as value type:

Static assertion failed due to requirement 'std::is_pointer<std::shared_ptr<Person>>::value': T must be a raw pointer type or a std::unique_ptr

benstadin avatar Mar 29 '24 13:03 benstadin

Yes, the reason is that the ramalhete_queue can only store raw pointers. For unique_ptr we can simply use release in push to get the raw pointer and take over ownership, and then create a new unique_ptr with that value in pop to hand over ownership again. For shared_ptr this does not work. A shared_ptr usually consists of two pointers, one to the control block, and one to the value. We can use shared_ptr::get to get a raw pointer to the value, but that pointer does not keep the value alive. Unfortunately the standard does not provide access to the control block, so there is no standard compliant way to do this (at least I am not aware of any).

mpoeter avatar Apr 05 '24 08:04 mpoeter