From: jbn@lulea.trab.se (Johan Bengtsson)
Newsgroups: comp.std.c++
Subject: Re: Pointer Comparisons
Date: 20 Dec 92 21:03:50 GMT
Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
X-Newsreader: TIN [version 1.1 + PL8]

John MAX Skaller (maxtal@extro.ucc.su.OZ.AU) wrote:

:   On Pointer Comparisons
:   ----------------------

: I propose
[...]

: 1) If two pointers of the same type, (other than void*) compare equal,
:    then they may be assumed to refer to the same object, subobject,
:    or component.

: Comment.

:    Allocations by 'new' of empty classes will then require at least one
:    byte of address space be wasted, otherwise
[...]
:    Inclusion of an empty class in a structure must also have this
:    property,[...]

==>	Only if adjacent to another member with which it has a common
	base class.

: Alternative.

:    The alternative rule allows an exception if the classes are empty,
:    empty meaning either no data members, or better, no data members
:    and no virtual functions.

==>	"no virtual functions" is perhaps too restrictive, since that
	might unnecessarily preclude implementations that don't use
	the "vtbl" scheme, for example by tagging all pointers with
	the actual type, or using special hardware (e.g. associative memory).

	I think "no data members" must be enough to void all
	guarantees on pointer distinctness (in this alternative
	to rule (1)).

:    This alternative precludes use of empty classes solely to create
:    unique objects with no operations, but appears to have no other
:    serious consequences. Since nonempty classes can always be
:    used to artificially create unique object identities, this
:    alternative may be viable.

==>	I would agree, but only for "vtbl" like implementations,
	since objects under future implementations may very well need
	zero storage, even if they have virtual functions. Since we
	do not wish to preclude future non-vtbl implementations
	(right?), I do _not_ think this alternate rule is a good one.

	I think the alternate rule (1) must be

	"You can't rely on pointer equivalence for classes that have
	no data members"

	disqualifying among others many abstract base classes (I know
	you John like MI mix-ins, many of those should be affected too).
	
[...]
[ definition of "domestic" pointers deleted ]

:    Two void* compare equal if they were derived from the two
:    valid domestic pointers that compare equal, and those
:    pointer values remain valid.

==>	Two void* should also be allowed to compare equal in
	cases where they are not derived from two domestic
	pointers that compare equal, i.e.

	p1==p2  ==>  (void*)p1==(void*)p2,

	but not the other way around.  The reverse should only hold
	if (p1 == p2) is defined, assuming equality comparison
	is only defined if p1 and p2 are of the same type, or share
	a common base class.

	Also note that the common base class of p1 and p2 must not
	be an empty class (if the alternative rule (1) is adopted).

: Comment

:    The desired equivalence

:    a==b <==> a,b refer to the same object
:    a!=b <==> a,b refer to different objects

:    is subject to the 3 constraints:

:    a) nonempty class (if rule 1 alternative is taken)
:    b) validity (continued existence of the object pointed to)
:    c) domesticity (exclusion of mangling and foreign suppliers)

==>	I like the analysis, although I think constraint a) should
	be removed, since all classes with no data members must be
	considered "empty".

-- 
--------------------------------------------------------------------------
| Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
| Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
--------------------------------------------------------------------------
Newsgroups: comp.std.c++