🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: implicit pointer of pointer argument promotion legal C++?

#38
From: Patrick Smith
Date: Wed, 04 Nov 1992 02:56
75 lines
3632 bytes
sdm@cs.brown.edu (Scott Meyers) writes:
|| >Hence, given that this is legal,
|| >
|| >    T *p;
|| >    void *q = p;
|| >
|| >is there a good reason why this isn't?
|| >
|| >    void f(T*);
|| >    void (*pf)(void*) = f;
|
|Clearly (I hope), if pf wants to do anything with its
|argument, it has to know what the real type is, i.e., it's going to have to
|do an explicit cast from void* to whatever the type really is.  Obviously
|this gives rise to the spector of type errors, but so does any use of void*
|pointers.  So my question is really whether there are any possibilities of
|type errors other than those inherent in the use of void* pointers.

Well, nobody seems to have pointed out any other semantic errors yet.
That's with the understanding that converting

   void (*) (T*)    to   void (*) (void*)

is equivalent to converting void* to T*, and both of which can
cause type errors and should require a cast.


Here's another way of looking at the question.
Ask yourself why one would want to declare pf this way:

   void (*pf)(void*);

If pf is only going to be called with T*'s as arguments, then it
would probably be better to declare pf as taking a T* argument
instead of a void*.  So when we see this declaration, we should
assume that pf may be called with more than one type of pointer
as an argument.  (I'm sure there are exceptions to this, but it
should be true as a general rule.)  In fact, the function to which
pf points may not even care what type of object its argument points to.

What if we write, as above,

   void f(T*);
   void (*pf)(void*) = f;

What should this indicate?

Since the parameter to pf is a void*, there are probably several
different pointer types which could be used as arguments to pf.
On the other hand, for so long as pf points to f, we'd better not
call pf with any pointer that's not a T*.  The compiler can't protect
us here, though; it has to accept any pointer as a parameter to pf.
So it's up to the program logic to guarantee that the argument
to pf will always be acceptable to the function pointed to by pf.

Situations such as this can easily cause subtle bugs.  Especially when
someone other than the original developer has to maintain the
program, perhaps years later.  The explicit cast is roughly
equivalent to a sign saying "Warning - there be dragons here!".
So I think it's a good idea that the cast be required.

(Now that I've said all that... I'm quite prepared to believe that
Scott has encountered a situation analogous to this, but where
pf will only be called with a T* for an argument and there are
good reasons for declaring pf to take a void* argument anyway.
Or that pf might take several different pointer types as arguments
and he's confident he will always get it right.  But I think most
of the rest of us would benefit from having the compiler force us
to do the equivalent of saying, "Yes, I know it looks strange, but
that's really what I meant".  I know I would.)

-- 
Patrick Smith
uunet.ca!frumious!pat
pat%frumious.uucp@uunet.ca
Technical Details
Message-ID: <1992Nov4.025639.620@frumious.uucp>
Path: arc1-ca.pugleaf.net!sparky!uunet!ogicse!emory!swrinde!zaphod.mps.ohio-state.edu!uunet.ca!frumious!pat
References: <1992Nov1.201547.4661@cs.brown.edu> <1d1oirINN3vu@agate.berkeley.edu> <1992Nov2.193135.6803@cs.brown.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!ogicse!emory!swrinde!zaphod.mps.ohio-state.edu!uunet.ca!frumious!pat
From: pat@frumious.uucp (Patrick Smith)
Newsgroups: comp.std.c++
Subject: Re: implicit pointer of pointer argument promotion legal C++?
Message-ID: <1992Nov4.025639.620@frumious.uucp>
Date: 4 Nov 92 02:56:39 GMT
Article-I.D.: frumious.1992Nov4.025639.620
References: <1992Nov1.201547.4661@cs.brown.edu> <1d1oirINN3vu@agate.berkeley.edu> <1992Nov2.193135.6803@cs.brown.edu>
Reply-To: uunet.ca!frumious!pat
Organization: None
Lines: 75