I don't see how that could happen though since from my understanding the only shared state in regards to the raw pointer is whether or not it's valid or not. I don't see how the value of the local raw pointer can be modified due to lifetime issues of the pointed to object.
shared_ptr<K> pk = make_shared<K>(...);
shared_ptr<G> pg = make_shared<G>(...);
Z z;
shared_ptr<Z> pz1 = shared_ptr<Z>(&z, pk);
shared_ptr<Z> pz2 = shared_ptr<Z>(&z, pg);
weak_ptr<Z> wp1 = pz1;
weak_ptr<Z> wp2 = pz2;
pk = nullptr;
pz1 = nullptr;
wp1 == wp2; //true
wp1.lock() == wp2.lock(); //falseOn quarta-feira, 22 de junho de 2016 12:11:16 PDT Nicol Bolas wrote:
> shared_ptr<K> pk = make_shared<K>(...);
> shared_ptr<G> pg = make_shared<G>(...);
> Z z;
> shared_ptr<Z> pz1 = shared_ptr<Z>(&z, pk);
> shared_ptr<Z> pz2 = shared_ptr<Z>(&z, pg);
Isn't this here invalid by itself?
I would say it has two mistakes:
* you have one object in two different shared_ptr that don't share the
refcounting internals
* you have a shared_ptr of an object that can't be destroyed by delete because
it was allocated on the stack
On quarta-feira, 22 de junho de 2016 07:57:31 PDT sai...@gmail.com wrote:
> I'm wondering why these aren't provided, I'm thinking I've missed some kind
> of issue since it's not present on weak_ptr, and they both to my surprise
> previously used control block for ordering. Simplified I'm trying to do the
> following:
>
> std::vector< std::weak_ptr< char > > resVec;
> std::shared_ptr< char > newRes(new char, [&](char* res) {
> resVec.erase(std::find(resVec.begin(), resVec.end(), res )); delete res; });
> resVec.push_back(newRes);
>
> This is obviously a no go since the raw pointer can't find the associated
> weak_ptr. It would seem I'd have to instead use an associative container,
> even though the weak_ptr already contains the raw pointer.
Since the weak_ptrs can asynchronously become disengaged, it's a bad idea to
have comparison operators on them.
On 22 June 2016 at 11:36, Thiago Macieira <thi...@macieira.org> wrote:On quarta-feira, 22 de junho de 2016 07:57:31 PDT sai...@gmail.com wrote:
> I'm wondering why these aren't provided, I'm thinking I've missed some kind
> of issue since it's not present on weak_ptr, and they both to my surprise
> previously used control block for ordering. Simplified I'm trying to do the
> following:
>
> std::vector< std::weak_ptr< char > > resVec;
> std::shared_ptr< char > newRes(new char, [&](char* res) {
> resVec.erase(std::find(resVec.begin(), resVec.end(), res )); delete res; });
> resVec.push_back(newRes);
>
> This is obviously a no go since the raw pointer can't find the associated
> weak_ptr. It would seem I'd have to instead use an associative container,
> even though the weak_ptr already contains the raw pointer.
Since the weak_ptrs can asynchronously become disengaged, it's a bad idea to
have comparison operators on them.That argument doesn't hold for equality comparison. If the pointers are equal, then you already have a shared_ptr to it (since you are using it for the comparison), and the weak_ptr cannot asynchronously become disengaged. The only other concern is the atomicity guarantees, but that could be addressed by comparing control block addresses.
That argument doesn't hold for equality comparison. If the pointers are equal, then you already have a shared_ptr to it (since you are using it for the comparison), and the weak_ptr cannot asynchronously become disengaged. The only other concern is the atomicity guarantees, but that could be addressed by comparing control block addresses.
This violates the principle I outlined: comparing control blocks will not yield the same result as comparing the actual pointers. So while doing shared_ptr<T> comparison compares the addresses of the `T`s, doing a weak_ptr<T> comparison would compare the control blocks. So `lhs < rhs` would be different from `lhs.lock() < rhs.lock()`.
On quinta-feira, 23 de junho de 2016 07:16:13 PDT Nicol Bolas wrote:
> This violates the principle I outlined: comparing control blocks will not
> yield the same result as comparing the actual pointers. So while doing
> shared_ptr<T> comparison compares the addresses of the `T`s, doing a
> weak_ptr<T> comparison would compare the control blocks. So `lhs < rhs`
> would be different from `lhs.lock() < rhs.lock()`.
A hypothetical operator< would only need to compare the actual pointers, not
the control blocks. The control blocks can be safely ignored because the
actual pointers must be part of the same data block (or one past the end of
it). Order of pointers that don't belong to the same block is UB.