🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#25
From: Patrick Smith
Date: Sat, 31 Oct 1992 21:34
52 lines
2540 bytes
sdm@cs.brown.edu (Scott Meyers) writes:
|In article <1992Oct30.051334.1016@frumious.uucp> uunet.ca!frumious!pat writes:
|| sdm@cs.brown.edu (Scott Meyers) writes:
|| |    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?
|| 
|| 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.
|
|This seems like a program error.  The caller is passing a function
|expecting const char* arguments, but the callee is using it as if it is
|expecting int* parameters.  I'm interested in the case where the caller and
|the callee agree on the types of what's being passed:
|
|    void f( int (*h)(const void*, const void*) ) {
|        h("Foo", "Bar");
|    }

Yes, the program using my version of f() is (probably) in error.

But when the compiler sees the call to f(), it doesn't (in the general
case) know which version of f() is being called.  If it's your version,
there's no problem.  If it's my version, there's probably something
wrong.

In my understanding, one of the principles of C++ is that any construct
which has the potential to violate the type system is illegal unless
an explicit cast is used.  So the explicit cast should be required in
the call to f() - not because the compiler knows there is a problem,
just because it _doesn't_ know there _isn't_.  This is exactly why
you need an explicit cast to convert a pointer to base class to
pointer to derived class, but not to cast from derived to base.

-- 
Patrick Smith
uunet.ca!frumious!pat
pat%frumious.uucp@uunet.ca
Technical Details
Message-ID: <1992Oct31.213412.356@frumious.uucp>
Path: arc1-ca.pugleaf.net!sparky!uunet!uunet.ca!frumious!pat
References: <1992Oct30.021207.3985@cs.brown.edu> <1992Oct30.051334.1016@frumious.uucp> <1992Oct31.031431.6863@cs.brown.edu>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++
Path: arc1-ca.pugleaf.net!sparky!uunet!uunet.ca!frumious!pat
From: pat@frumious.uucp (Patrick Smith)
Subject: Re: implicit pointer of pointer argument promotion legal C++?
Message-ID: <1992Oct31.213412.356@frumious.uucp>
Date: Sat, 31 Oct 1992 21:34:12 GMT
Reply-To: uunet.ca!frumious!pat
References: <1992Oct30.021207.3985@cs.brown.edu> <1992Oct30.051334.1016@frumious.uucp> <1992Oct31.031431.6863@cs.brown.edu>
Organization: None
Lines: 52