Is the address-of operator (&) really needed?

deadalnix deadalnix at gmail.com
Fri Jun 1 05:28:29 PDT 2012


Le 31/05/2012 11:58, Dejan Lekic a écrit :
> On Thu, 31 May 2012 11:36:47 +0200, Sandeep Datta wrote:
>
>> Hi,
>>
>> I was going through some sample code online and came across the
>> following code fragment...
>>
>> 	listenHttp(settings,&handleRequest); //Where handleRequest is a
>> function
>>
>> My question to you is (as the title says) is the address-of operator (&)
>> really needed here? Wouldn't it be better to consider handleRequest to
>> be a reference to the actual function? I think this will make the system
>> consistent with the way variables work in D. IMO this will bring
>> functions/delegates closer to being first class objects in D.
>>
>> What do you think?
>>
>> Regards,
>> Sandeep Datta.
>
> It is needed.
>
> Consider this example:
>
> import std.stdio;
>
> /*
> float handleRequest() {
>    return 1.0f;
> }
> */
>
> int handleRequest() {
>    return 200;
> } // handleRequest() function
>
> int main() {
>    int function() fptr;
>    //fptr = handleRequest; // will not work, because it is "understdood"
> as:
>                            // fptr = handleRequest();
>
>    fptr =&handleRequest;  // This will work if we have only one
> handleRequest();
>                            // If you uncomment the first one, you are in
> trouble
>
>    int val = handleRequest; // calls handleRequest() actualy
>
>    //listenHttp(settings, fptr); // no need for&  because fptr is an
> object of "int function()" type
>
>    writeln(val); // OUTPUT: 200
>
>    return 0;
> } // main function
>
>
>

This behavior is planed to be deprecated. The & behavior should go as 
well I think.


More information about the Digitalmars-d mailing list