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

Jonathan M Davis jmdavisProg at gmx.com
Thu May 31 08:40:51 PDT 2012


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


More information about the Digitalmars-d mailing list