Is there a way to create compile-time delegates?

torhu no at spam.invalid
Mon Jul 19 13:01:30 PDT 2010


On 19.07.2010 21:06, Simen kjaeraas wrote:
> Yeah, what the subject says.
>
> I want to have a default delegate for a struct, and without a default
> constructor, this has to be a compile-time constant. Now, logically,
> there should be nothing wrong with storing the function pointer and a
> null context pointer at compile-time, but it seems there is. Any ideas?
>
> struct foo {
>       void delegate( ) dg = () {}; // Error: non-constant expression
>                                    // __dgliteral1
> }
>

I wasn't able to make it work.  The compiler probably sees delegates as 
something that just can't be created at compile time, since no runtime 
contexts exist then.  Which is reasonable.

Maybe one of those templates that turn functions into delegates will 
work?  Otherwise I guess you're back to using a factory function for 
initializing instances.

Maybe just checking for null pointers before calling those delegates 
ends up being the easiest solution.


More information about the Digitalmars-d-learn mailing list