Topic: Overloaded func addr in cast


Author: daniel@cse.ucsc.edu (Daniel R. Edelson)
Date: 4 Dec 1992 18:59:15 GMT
Raw View
Does the draft standard require a compiler to take the
address of an overloaded function in a cast-expression?
Please consider the following code?

    void f(int);
    void f(void*);

    typedef void (*fp)(int);

    void * p = fp(f);  // address of overloaded function?

    int main(void) { return 0; }

Section 13.3 of the draft Standard specifies 5 ways to take the
address of an overloaded function:
   1) an object being initialized
   2) the left side of an assignment
   3) a formal argument of a function
   4) a formal argument of a user-defined operator
   5) a function return type

Items 2 through 5 don't apply in this case. Item 1 applies
if an only if the compiler generates a temporary
object for the source of the assignment. If there is such a
temporary, the address of the overloaded function is taken to
initialize the temporary. However, if the compiler does not
generate a temporary, then it seems to me the compiler is not
required to take the address of the overloaded function. In
that case this code would be erroneous (due to ambiguiy).

Just to make the code slightly more interesting, replace the
pair of overloaded functions with:
 template<class T> void f(T) { }

Now, can someone identify language in the draft standard that either:
 1) requires the compiler to generate a temporary, making
    the code always legal for that reason, or,
 2) makes the code always legal for another reason.


Thanks very much,
Daniel Edelson
daniel@cse.ucsc.edu




Author: pkt@lpi.liant.com (Scott Turner)
Date: Mon, 7 Dec 1992 14:53:05 GMT
Raw View
In article <1fo9q3INN1fo@darkstar.UCSC.EDU>, daniel@cse.ucsc.edu (Daniel R. Edelson) writes:
|> Does the draft standard require a compiler to take the
|> address of an overloaded function in a cast-expression?
|> Please consider the following code?
|>
|>     void f(int);
|>     void f(void*);
|>
|>     typedef void (*fp)(int);
|>
|>     void * p = fp(f);  // address of overloaded function?

|>   1) an object being initialized

|> Item 1 applies
|> if an only if the compiler generates a temporary
|> object for the source of the assignment. If there is such a
|> temporary, the address of the overloaded function is taken to
|> initialize the temporary.

If you read the working paper that way, then as you suggest
there is no answer.  It seems more reasonable to me that where
it says "an object being initialized (8.4)" it is talking
about the explicit initializations which are the subject of 8.4.
This would be consistent with the general intent to minimize
implementation dependencies.  The creation of temporaries should
not come into it.

So I hope no one can "identify language in the draft standard that"
"makes the code always legal".  It should always be illegal.

The extensions working group is working on refinements to the
cast expressions, including a kinder, gentler explicit conversion
which would be more appropriate for the purpose of the
example above.
--
Prescott K. Turner, Jr.
Liant Software Corp. (developers of LPI languages)
959 Concord St., Framingham, MA 01701 USA    (508) 872-8700
UUCP: uunet!lpi!pkt                          Internet: pkt@lpi.liant.com