Same address, multiple shared_ptr counters, is it forbidden by C++ standard?

c++, c++11, language-lawyer, shared-ptr

Solution

I can't find anything in the standard (well, the final draft) that specifically rules it out. The closest I can find is a note in `20.9.11.2.10 shared_ptr casts`

5 [ Note: The seemingly equivalent expression shared_ptr(static_cast(r.get())) will eventually result in undefined behavior, attempting to delete the same object twice. —end note ]

which actually seems to forget about your case with a custom deleter.

Problem

Suppose I have the need to do the following (This is just some imaginative code for discussion of the C++ standard, thus I won't discuss why I design it this way, so don't bother me with something like: your design is wrong.) ``` T* ptr = new T; shared_ptr<T> p(ptr); shared_ptr<T> q(ptr, SomeDeleterThatDoesnotDeleteButDoSomeOtherStuff()); ``` Suppose the logic guarantees that `p` or some of its copies lives longer than all copies of `q`, so practically there won't be any problem. My question is, is it forbidden by C++ standard, e.g. explicitly stated as UB by C++ standard, for different shared_ptr counters to share the same address? Thanks.

Original source