Rationale for shared_ptr/array including both ptr to control and ptr to data in class

Hi, I was wondering why shared_ptr/array include both a ptr to the control block and a ptr to the data (ptr) in the class itself. I assume it's for performance reasons, but I didn't find anything about that in the docs. Greetings, Olaf

Sebastian Redl wrote:
It's true that the separate pointer enables aliasing, but this is not the original reason for its existence. It supports pointer conversions (shared_ptr<T> converts to shared_ptr<U> when T* converts to U*.) shared_array doesn't technically need aliasing or conversions, its implementation just mirrors shared_ptr.

Olaf van der Spek wrote:
Ah. BTW, why doesn't shared_array provide size()? IMO it'd be quite useful to have.
The obvious answer, that it doesn't have this information, has already been given, but it's not enough; you could legitimately ask why shared_array doesn't have it, that is, why its constructor doesn't take a size in addition to the pointer. The answer is, well, historical reasons. While the current shared_ptr allows you to easily implement a sized shared_array by storing the size in a deleter, when shared_array was first designed, this was not the case. shared_array was kept a simple reference-counted wrapper over new T[]. shared_ptr then evolved considerably, and shared_array has failed to keep up. So here we are. :-)
participants (7)
-
Mathias Gaunard
-
Nathan Ridge
-
Olaf van der Spek
-
Peter Dimov
-
Robert Kawulak
-
Roman Perepelitsa
-
Sebastian Redl