Thread overview
Implicit Casts of Delegates & Function Pointers
Feb 27, 2004
Russ Lewis
Feb 27, 2004
Matthew
Feb 28, 2004
Ben Hinkle
Mar 02, 2004
Russ Lewis
February 27, 2004
IMHO, a delegate or function should be implicitly castable to another function pointer or delegate type if the types would also allow implicit casting.  More formally:

	A0 delegate(A1, A2, A3, ... , An)
should be implicitly castable to
	B0 delegate(B1, B2, B3, ... , Bn)
if and only if the following conditions are met:
1) A0 is implicitly castable to B0
2) For all i, Bi is implicitly castable to Ai.
Note that in (2), the direction of casting is reversed.



Here's a practical example:

class Base {};
class Child : Base {};
void myFunc(Base b) {};
int main() {
	void function(Child) foo = &myFunc;
	return 0;
}

The compiler currently chokes on the assignment.  However, the assignment that makes sense.  We have a function that requires that its argument be a Base, or child of Base.  We have a function pointer which, when it is called, must be called with something that is (at least) a
Child.  Thus, any time that the function pointer is called, we know that myFunc will be getting a child of Base.

Thus, from my view, the implicit cast should be legal.

Thoughts?

February 27, 2004
No, but it should be explicitly castable. I presume it is already ...

"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:c1og80$dff$1@digitaldaemon.com...
> IMHO, a delegate or function should be implicitly castable to another function pointer or delegate type if the types would also allow implicit casting.  More formally:
>
> A0 delegate(A1, A2, A3, ... , An)
> should be implicitly castable to
> B0 delegate(B1, B2, B3, ... , Bn)
> if and only if the following conditions are met:
> 1) A0 is implicitly castable to B0
> 2) For all i, Bi is implicitly castable to Ai.
> Note that in (2), the direction of casting is reversed.
>
>
>
> Here's a practical example:
>
> class Base {};
> class Child : Base {};
> void myFunc(Base b) {};
> int main() {
> void function(Child) foo = &myFunc;
> return 0;
> }
>
> The compiler currently chokes on the assignment.  However, the assignment that makes sense.  We have a function that requires that its argument be a Base, or child of Base.  We have a function pointer which, when it is called, must be called with something that is (at least) a Child.  Thus, any time that the function pointer is called, we know that myFunc will be getting a child of Base.
>
> Thus, from my view, the implicit cast should be legal.
>
> Thoughts?
>


February 28, 2004
"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message
news:c1og80$dff$1@digitaldaemon.com...
| IMHO, a delegate or function should be implicitly castable to another
| function pointer or delegate type if the types would also allow implicit
| casting.  More formally:
|
| A0 delegate(A1, A2, A3, ... , An)
| should be implicitly castable to
| B0 delegate(B1, B2, B3, ... , Bn)
| if and only if the following conditions are met:
| 1) A0 is implicitly castable to B0
| 2) For all i, Bi is implicitly castable to Ai.
| Note that in (2), the direction of casting is reversed.

There is a problem when the implicit cast changes the size
of the data. For example, if a variable is declared
 void delegate(int)
then to call the delegate D pushes an int onto the stack
and calls. If the function it calls is declared to take a
byte then it will look on the stack at the wrong place.
Matching up with your proposal it would be
 A0 = void, A1 = int, B0 = void, B1 = byte

This makes me lean towards forcing the cast to be
explicit.

-Ben


March 02, 2004
You're right.  We should add the additional requirement that you can only implicitly cast if the types have the same .size.

What I'm most interested in is the base class/derived class example I posted.  Everything else is just gravy :)

Russ

Ben Hinkle wrote:
> "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message
> news:c1og80$dff$1@digitaldaemon.com...
> | IMHO, a delegate or function should be implicitly castable to another
> | function pointer or delegate type if the types would also allow implicit
> | casting.  More formally:
> |
> | A0 delegate(A1, A2, A3, ... , An)
> | should be implicitly castable to
> | B0 delegate(B1, B2, B3, ... , Bn)
> | if and only if the following conditions are met:
> | 1) A0 is implicitly castable to B0
> | 2) For all i, Bi is implicitly castable to Ai.
> | Note that in (2), the direction of casting is reversed.
> 
> There is a problem when the implicit cast changes the size
> of the data. For example, if a variable is declared
>  void delegate(int)
> then to call the delegate D pushes an int onto the stack
> and calls. If the function it calls is declared to take a
> byte then it will look on the stack at the wrong place.
> Matching up with your proposal it would be
>  A0 = void, A1 = int, B0 = void, B1 = byte
> 
> This makes me lean towards forcing the cast to be
> explicit.
> 
> -Ben
> 
>