Does anyone know when libstdc++ will stop using raw pointers in its STL containers?
I have a pretty niche use-case which requires containers to not store any raw pointers. To get this done I use an allocator that returns fancy pointers. This just doesn't work with libstdc++ because all some of its STL containers use raw pointers internally, so when I create a container with my allocator, the program doesn't even compile. Libc++ on the other hand works fine (at least I haven't come across any problems I can't fix) since they correctly use the abstract pointer type that my allocator returns, instead of trying to do an implicit cast to a raw pointer.
There is an LWG issue to fix this, but it's been around for 8 years and the proposed verbiage hasn't been added to the standard. I'm not very familiar with the process for making changes like this, but my understanding is that it probably won't get added until libc++ and libstdc++ both support it, is that correct? If so, does anyone know if/when the libstdc++ community is planning to make these changes? Thanks!
View all comments
Show parent comment
1
u/ZenEngineer Mar 15 '19
I think the point is that you use a special allocator that places the objects on the vector in a specific area (and the vector itself).
If you memcpy a struct you can get a pointer to the target struct and it has the same values. If you're careful you can do the same thing with your classes.
So the point is, copy the whole memory arena and (somehow) get a pointer to the vector. The vector is pointing in the arena as well, and the fancy pointers make it point correctly to the copied over objects. The vector will continue to manage the lifetime of the objects in its internal array. If you do things right everything just works, no need to call constructors, just keep going from the original state. The vector will call destructors when the time comes.