Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shared_ptr do not own a deleter for non-array type #2560

Closed
EzoeRyou opened this issue Dec 4, 2018 · 2 comments
Closed

shared_ptr do not own a deleter for non-array type #2560

EzoeRyou opened this issue Dec 4, 2018 · 2 comments

Comments

@EzoeRyou
Copy link
Contributor

EzoeRyou commented Dec 4, 2018

It is agreed that the shared_ptr calls the appropriate non-virtual destructor.

struct Base { ~Base() {} } ;
struct Derived : Base { ~Derived() { } } ;

std::shared_ptr<Base> p( new Derived() ) ;
// call Derived's destructor on destruction of p

Current wording cannot be interpreted as such for non-array type.

At [util.smartptr.shared.const], of the constructor:

template<class Y> explicit shared_ptr(Y* p);

Effects clause stated that:

When \tcode{T} is not an array type,
constructs a \tcode{shared_ptr} object
that owns the pointer \tcode{p}.
Otherwise, constructs a \tcode{shared_ptr}
that owns \tcode{p} and a deleter of an
unspecified type that calls \tcode{delete[] p}.

Notice the period at line 3. I interpret this two sentences as follows.

  1. For non-array type T, it just own a pointer and nothing else. It does not own a deleter which conveniently call delete p where type of p is Y *.
  2. For array type T, it own a pointer and a deleter which calls delete[] p where the type of p is Y *.

Since it doesn't own a deleter for non-array type T, the shared_ptr destructor simply calls delete p as stated in [util.smartptr.shared.dest].

\item Otherwise, \tcode{*this} owns a pointer \tcode{p},
and \tcode{delete p} is called.

Destructor's wording simply says p is "a pointer p", it doesn't say what type it is. If this wording is suffice, we can remove a deleter for array type T.

The proposed fix is, change the first sentence of quoted Effects clause as follows.

When \tcode{T} is not an array type,
constructs a \tcode{shared_ptr} object
that owns the pointer \tcode{p} and a deleter of an
unspecified type that calls \tcode{delete p}.

A side note. Non-array type said "owns the pointer `tcode{p}", array type said "owns \tcode{p}". It's inconsistent. It should be consistent.

@EzoeRyou EzoeRyou changed the title shared_ptr do not own a deleter for non-array pointer shared_ptr do not own a deleter for non-array type Dec 4, 2018
@jwakely
Copy link
Member

jwakely commented Dec 4, 2018

If this wording is suffice, we can remove a deleter for array type T.

I'm not sure what you're saying here.

The proposed fix is, change the first sentence of quoted Effects clause as follows.

Why? There is no problem with the current wording.

A side note. Non-array type said "owns the pointer `tcode{p}", array type said "owns \tcode{p}". It's inconsistent. It should be consistent.

No, the difference is intentional. It mirrors the wording in the destructor where it says it owns "an object p and a deleter d" or "owns a pointer p".

@EzoeRyou
Copy link
Contributor Author

EzoeRyou commented Dec 4, 2018

  1. Right. For array case, we need to use delete[] p instead of delete p.
  2. Hmm
  3. I see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants