Constant function/delegate literal

Timon Gehr timon.gehr at gmx.ch
Tue Jan 17 09:54:42 PST 2012


On 01/17/2012 05:02 PM, Don Clugston wrote:
> On 15/01/12 20:35, Timon Gehr wrote:
>> On 01/14/2012 07:13 PM, Vladimir Matveev wrote:
>>> Hi,
>>>
>>> Is there a reason why I cannot compile the following code:
>>>
>>> module test;
>>>
>>> struct Test {
>>> int delegate(int) f;
>>> }
>>>
>>> Test s = Test((int x) { return x + 1; });
>>>
>>> void main(string[] args) {
>>> return;
>>> }
>>>
>>> dmd 2.057 says:
>>>
>>> test.d(7): Error: non-constant expression cast(int
>>> delegate(int))delegate pure
>>> nothrow @safe int(int x)
>>> {
>>> return x + 1;
>>> }
>>>
>>> ?
>
> The 'this' pointer in the delegate:
> Test((int x) { return x + 1; });
>
> is a pointer to the struct literal, which isn't constant.
> You actually want it to point to variable s.


The struct literal residing in static storage has a variable address?
Why would the context pointer point to the struct literal anyway? It is 
not required.

>
> OTOH if it were a function, it should work, but it currently doesn't.
>

It should even work for delegates. The context pointer would just be null.



More information about the Digitalmars-d-learn mailing list