🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1243

Re: Pointer comparisons and templates

#1243
From: Johan Bengtsson
Date: Tue, 15 Dec 1992 01:14
41 lines
2269 bytes
Dag Bruck (dag@bellman.control.lth.se) wrote:
: In the recent discussion on pointer comparisons, Fergus Hendersson
: suggested adding a standard library function ptrcmp() instead of
: extending the definitions of "<" and ">" for pointers.  I think we
: also agreed on a reasonable relationship between ptrcmp() and the
: relational operators.

: While I think ptrcmp() is better than nothing, I still think extending
: the definition of the relational operators is far better.  Consider
: this template class (the code may be wrong, I'm just inventing an
: almost real example for the purpose of discussion):

[ class template needing total ordering on objects _and_ pointers deleted ]

: Now, here's the problem: this implementation works fine for all
: element types that have an "operator < ()" including plain built-in
: types and many user-defined types.  However, it is not guaranteed to
: work with pointer elements for reasons we have discussed before.

: They way around this problem is to make a specialization for pointers:

Dag, there is another way:

Have the class template rely on a function template isless() instead,
with a specialization for pointers:

template<class T> inline int isless(T a, T b)
{ return a < b; }

inline int isless( const void* a, const void* b )
{ return ptrcmp(a,b) < 0; }

That ought to do it.  NOTE: I don't yet have a compiler that does
function templates, so this is untested.  Perhaps someone with a
more real compiler can check that this really works?

-- 
--------------------------------------------------------------------------
| Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
| Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
--------------------------------------------------------------------------
Technical Details
Message-ID: <5376@miramon.lulea.trab.se>
Path: arc1-ca.pugleaf.net!sparky!uunet!enterpoop.mit.edu!eru.mt.luth.se!hagbard!loglule!jbn
References: <1992Dec14.075853.3399@lth.se>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!enterpoop.mit.edu!eru.mt.luth.se!hagbard!loglule!jbn
From: jbn@lulea.trab.se (Johan Bengtsson)
Newsgroups: comp.std.c++
Subject: Re: Pointer comparisons and templates
Message-ID: <5376@miramon.lulea.trab.se>
Date: 15 Dec 92 01:14:31 GMT
References: <1992Dec14.075853.3399@lth.se>
Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
Lines: 41
X-Newsreader: TIN [version 1.1 + PL8]