[Issue 1069] No error on type mismatch with varargs

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 19 13:14:16 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1069





------- Comment #1 from fvbommel at wxs.nl  2007-03-19 15:14 -------
Actually, I think the compiler may be right here. Your code invokes undefined
behavior but is otherwise legal and the compiler has every right to
successfully compile this into a program that crashes without complaining.

Let me explain:
What you're seeing is an unfortunate side-effect of the combination of lazy
[void delegate()] arguments with the fact that a function name doesn't evaluate
to a pointer/delegate to that function but to its return value (i.e. property
syntax).
Your 'add( fn );' adds an implicit delegate that calls this.dg() (because of
property syntax). (It's equivalent to 'add( { fn() } );')
Since this implicit delegate refers to the stack frame of the constructor, it's
invalid after the constructor returns. Your call() then calls an invalidated
delegate, and your program crashes.

So as you mentioned, to do what you want you should use 'add( &fn );'.
What you didn't realize was that the _reason_ this works is that you're then
passing a method delegate instead of an (anonymous) inner function delegate.


-- 



More information about the Digitalmars-d-bugs mailing list