Page 1 of 1

C++11: Factory allocators?

Posted: Thu Jun 19, 2014 8:35 am
by beneficii
Let's say that I have a base class, with many classes derived through inheritance, and I want to store all instances of the base/derived classes in a container storing, say, smart pointers of the type std::unique_ptr<base>, which stores a pointer to the base class. It seems that with the addition of the emplace() methods to the STL, it would be feasible, perhaps even more efficient than otherwise, to write a custom allocator whose construct() method acts as a factory for producing a pointer of the correct base/derived class and then setting the std::unique_ptr<base> smart pointer that is being constructed inside the container to point to the base/derived class.