On Sunday, 12 April 2015 20:03:44 UTC+3, Norman J. Goldstein wrote:
> Thanks for all the responses. I will address all the comments and
> clarify, further.
>
> On Sunday, 12 April 2015 11:20:06 UTC+3, Marcel Mueller wrote:
> > On 12.04.15 09.57, Norman J. Goldstein wrote:
> >> I would find it helpful for a shared_ptr to have an id() method, so that
> >> two shared_ptr's manage the same object only if and only if they have
> >> the same id's. If the managed object is not NULL, then the address of
> >> the object can work as an id. Obviously, this does not work if the
> >> object is NULL.
> >
> > Why not?
> >
> > If a pointer does not point to an object then there is no definition of
> > "the same object".
> >
> > And it is common practice and sometimes very useful that
> > reference-equals comparisons return true if both operands point to the
> > same object /or/ both are NULL.
> *> Yes, that is common practice.
>
> In the case of shared_ptr's, it is wrong, in my situation, to take a
> NULL pointer as a shared reference. I am interested only in the
> relationship reflected by the use_count().
The 'use_count()' is required to return zero when 'shared_ptr' is empty.
So if you are not interested in the situation (for example since there
is nothing further to serialize) then why you care if they compare equal
or no?
> A NULL pointer has an address, say, of 0, so this cannot be used to
> determine whether two shared_ptr's are managing the same object. If one
> of the two shared_ptr's does reset( new T ), then the two shared_ptr's
> will both be managing a bona fide same object. So, even if the managed
> object is NULL, the relationship between the two shared_ptr's is
> maintained. The use_count() is (at least) two for the two shared_ptr's,
> even if they are sharing only NULL.
Every 'shared_ptr<X>' is separate and if one starts to point at
something else then others don't.
> On 04/12/2015 06:29 AM, Öö Tiib wrote:
Following was written by Paavo Helde:
> >> Also, conceptually I do not get why such a feature would be useful. A
> >> smartpointer becomes NULL only if I assign NULL to it. Why should it then
> >> remember to which object it pointed earlier? If I wanted to remember that,
> >> I would not have assigned NULL to it in the first place.
> >
>
> As pointed out, above, shared_ptr's do remember the relationship, even
> if they are assigned NULL. The point I am making is that I would like
> to be able to query that relationship, even if the pointer value is NULL.
They aren't required to remember any relationships; empty 'shared_ptr'
is empty regardless if the object at what it pointed still exists
or not. I am not sure what relationship.
That was written by me:
> > Maybe what he wants is something achievable with
> > 'std::shared_ptr::owner_before' or 'std::shared_ptr::owner_less'. He did
> > not describe it too well.
>
> I'm sorry, I don't understand these suggestions. The reason that I want
> the id() feature, is so that I can save shared_ptr's to disk, and when
> they get restored to RAM, they maintain the same relationship.
http://isocpp.org/wiki/faq/serialization
The 'shared_ptr' is like ordinary pointer that can point at object at
what other pointers can point too. It does not provide indexing
services. So you have to maintain some map (for example like
'std::map<std::weak_ptr<X>,unsigned>' or 'std::map<X const*,unsigned>')
to map the shared object you serialize to some numeric id.
> Currently, when the shared_ptr's are NULL, I cannot do this. During the
> running of the program, one of the shared_ptr's might do a reset( new T
> ), and all the (intended) shared_ptr's must see the change.
I do not understand. Lets imagine there are two 'shared_ptr's p1 and p2
and two objects o1 and o2. All the 9 combinations of states of
pointers is on following diagram:
p1 | p2
--------------
1) null | null
2) null | o1
3) null | o2
4) o1 | null
5) o1 | o1
6) o1 | o2
7) o2 | null
8) o2 | o1
9) o2 | o2
What is the difference for p2 if the state transits from 1) to
4) or if the state transits from 5) to 8)? It can't see anything
anyway ... it still is empty or points at o1 and it does not know
where p1 points. Please clarify where is difference and why they
should not compare equal in state 1).