Article View: comp.std.c++
Article #27Re: implicit pointer of pointer argument promotion legal C++?
From: Joe Buck
Date: Sun, 01 Nov 1992 23:17
Date: Sun, 01 Nov 1992 23:17
37 lines
1545 bytes
1545 bytes
In article <1992Nov1.201547.4661@cs.brown.edu> sdm@cs.brown.edu (Scott Meyers) writes: >However, you don't need an explicit cast to go from a T* to a void*. >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; Certainly. Consider the line that would be legal next: T2* p; // class T2 has no relation to class T pf(p); Now we have passed a pointer to T2 to a function that thinks it is getting a pointer to T, and we have broken the type system. It turns out that you've gotten things exactly backwards. It would be safer (although there is still a problem) to have void f(void*); void (*pf)(T*) = f; Why is this better? Because we can only pass T* pointers as arguments to pf, and these are guaranteed to be legal to cast to void*. Why is it still a problem? Because void* and T* may have different representations. -- Joe Buck jbuck@ohm.berkeley.edu
Technical Details
Message-ID:
<1d1oirINN3vu@agate.berkeley.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!wupost!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!spool.mu.edu!agate!forney.berkeley.edu!jbuckReferences:
<1992Oct31.031431.6863@cs.brown.edu> <1992Oct31.213412.356@frumious.uucp> <1992Nov1.201547.4661@cs.brown.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!wupost!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!spool.mu.edu!agate!forney.berkeley.edu!jbuck From: jbuck@forney.berkeley.edu (Joe Buck) Newsgroups: comp.std.c++ Subject: Re: implicit pointer of pointer argument promotion legal C++? Date: 1 Nov 1992 23:17:47 GMT Organization: U. C. Berkeley Lines: 37 Message-ID: <1d1oirINN3vu@agate.berkeley.edu> References: <1992Oct31.031431.6863@cs.brown.edu> <1992Oct31.213412.356@frumious.uucp> <1992Nov1.201547.4661@cs.brown.edu> NNTP-Posting-Host: forney.berkeley.edu