Article View: comp.std.c++
Article #14Re: implicit pointer of pointer argument promotion legal C++?
From: Patrick Smith
Date: Fri, 30 Oct 1992 05:13
Date: Fri, 30 Oct 1992 05:13
46 lines
2078 bytes
2078 bytes
sdm@cs.brown.edu (Scott Meyers) writes:
|I've been wondering about a similar question. Is there any danger of
|allowing the "usual" conversions of pointers and references in parameter
|types of function pointers? Example:
|
| void f( int (*)(const void*, const void*) );
|
| int g(const char*, const char*);
|
| f( g ); // error
| f (int (*)(const void*, const void*) g ); // fine
|
|If the usual T* ==> void* conversion were allowed for function pointer
|parameters, no cast would be required when calling f with g. Is there a
|lurking danger here?
I think you have this the wrong way around. The example above is
dangerous. If f() is
void f( int (*h)(const void*, const void*) ) {
int x;
h( &x, &x );
}
you wind up converting an int* to a void* and then to a const char*.
Such a conversion should require an explicit cast at some point.
But if you do it the other way,
void f( int (*)(const char*, const char*) );
int g(const void*, const void*);
f( g ); // Should this be allowed?
there might not be any semantic problems.
However, if the conversion from const char* to const void* involves
any change to the actual representation of the pointer, then the
compiler may need to generate a small function to use as the
actual argument to f(). This function would just convert its
parameters from const char* to const void* and then call g.
--
Patrick Smith
uunet.ca!frumious!pat
pat%frumious.uucp@uunet.ca
Technical Details
Message-ID:
<1992Oct30.051334.1016@frumious.uucp>Path:
arc1-ca.pugleaf.net!sparky!uunet!ogicse!uwm.edu!zaphod.mps.ohio-state.edu!uunet.ca!frumious!patReferences:
<23999@alice.att.com> <1992Oct29.195546.1@vax1.bham.ac.uk> <1992Oct30.021207.3985@cs.brown.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!ogicse!uwm.edu!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: <1992Oct30.051334.1016@frumious.uucp> Date: 30 Oct 92 05:13:34 GMT Article-I.D.: frumious.1992Oct30.051334.1016 References: <23999@alice.att.com> <1992Oct29.195546.1@vax1.bham.ac.uk> <1992Oct30.021207.3985@cs.brown.edu> Reply-To: uunet.ca!frumious!pat Organization: None Lines: 46