5

I want to preallocate memory both for control block and value_type for shared_ptr in one heap request (like std::make_shared), but do not construct immediately any object in it. And when I actually need to construct object use placement new. Is it possible? Neither std::make_shared or std::allocate_shared seem to solve my problem.

Eliad
  • 894
  • 6
  • 20
  • 1
    Polymorphic allocators might be what you're looking for - coming to a C++ standard in the future. http://en.cppreference.com/w/cpp/experimental/lib_extensions – marko Mar 05 '16 at 14:31

1 Answers1

2

I would propose to create postponed initialization wrapper class that contains enough memory within itself for object you want to initialize later. This wrapper could even have special method for invoking placement new and delete initialized object in destructor.

George Sovetov
  • 4,942
  • 5
  • 36
  • 57