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

allocator_delete does not support polymorphism #33

Closed
twiggler opened this issue Apr 15, 2018 · 3 comments
Closed

allocator_delete does not support polymorphism #33

twiggler opened this issue Apr 15, 2018 · 3 comments

Comments

@twiggler
Copy link

Cannot convert
std::unique_ptr<Game::KillShip, foonathan::memory::allocator_deleter<Game::KillShip, foonathan::memory::temporary_allocator, std::mutex>>
TO
std::unique_ptr<Game::EntityCommand, foonathan::memory::allocator_deleter<Game::EntityCommand, foonathan::memory::temporary_allocator, std::mutex>

where Game::EntityCommand is the base class of Game::KillShip

@foonathan
Copy link
Owner

I can't really implement it, because deallocation of custom allocators is not polymorphic.

You see, when deallocating an object I need to know its size, which I don't know for a base class.
It only works for new and delete because they are built-in language features and can do magic (for example, the destructor could return sizeof(*this)).

What I could add is a allocator_polymorphic_deleter that stores the size as an additional member variable but this has overhead. Would that work for you?

I'm assuming you're having a container of unique_ptr<Base> and want to put derived classes in it? This is not necessarily good design as all memory::unique_ptr store a reference to an allocator, even though they may just be allocated from the same one. Something like a ptr_vector that stores only one reference allocator would be more efficient anyway.

@twiggler
Copy link
Author

twiggler commented Apr 18, 2018 via email

@twiggler
Copy link
Author

Thanks!

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