Is the address-of operator (&) really needed?
Dejan Lekic
dejan.lekic at gmail.com
Fri Jun 1 08:20:43 PDT 2012
On Thu, 31 May 2012 08:40:51 -0700, Jonathan M Davis wrote:
> On Thursday, May 31, 2012 11:36:47 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?
>
> 1. It's needed so that you can call it when calling C code.
>
> 2. Just because ref is often better than a pointer doesn't mean that
> it's never valuable to be able to pass a pointer to a variable.
>
> 3. ref doesn't work with variadic templates very well. Take a look a
> std.getopt.getopt. It takes pointers, not refs, and there isn't a way to
> make it take refs.
>
> 4. & is useful for getting function pointers.
>
> There is _zero_ roason to get rid of & IMHO.
>
> - Jonathan M Davis
+1 100%
I would add that "fptr = &function;" makes it _clear_ what is going on
there, otherwise I would have to go and find what "function" is...
--
Dejan Lekic
mailto:dejan.lekic(a)gmail.com
http://dejan.lekic.org
More information about the Digitalmars-d
mailing list