Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for shared_ptr values #34

Open
benstadin opened this issue Mar 29, 2024 · 1 comment
Open

Support for shared_ptr values #34

benstadin opened this issue Mar 29, 2024 · 1 comment

Comments

@benstadin
Copy link

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

@mpoeter
Copy link
Owner

mpoeter commented Apr 5, 2024

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants