Topic: Is there a serious reason for not allowing references to methods?


Author: Joe Keane <jgk@jgk.org>
Date: 1999/05/27
Raw View
In article <37446692.CDA4ABFB@technologist.com>
David R Tribble <dtribble@technologist.com> writes:
>It's an semantic shorthand of C++ (and C) that lets you omit the '&'
>and '*' operators when dealing with function pointers,

That's not in C originally, it's an ANSI extension.

And if you ask me, it screws things up for no reason.

--
Joe Keane, amateur mathematician
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/05/28
Raw View
On 27 May 99 07:38:05 GMT, Joe Keane <jgk@jgk.org> wrote:
>David R Tribble <dtribble@technologist.com> writes:

>>It's an semantic shorthand of C++ (and C) that lets you omit the '&'
>>and '*' operators when dealing with function pointers,
>
>That's not in C originally, it's an ANSI extension.
>
>And if you ask me, it screws things up for no reason.

Ommitting the '*' when calling a non-member function by address is
necessary for template functions to work on function objects or
functions.  Eg,
   template <class Iter, class Action>
   Action for_each(Iter begin, Iter end, Action);
We observe that 'Action' can be either a function object or a pointer
to function.  This means that when the implementation of for_each says
   action(*begin++);
this expression should work regardless of whether Action is a function
object or a function pointer.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/05/22
Raw View
On 21 May 1999 23:34:07 GMT, David R Tribble <dtribble@technologist.com> wrote:

>It's an semantic shorthand of C++ (and C) that lets you omit the '&'
>and '*' operators when dealing with function pointers, even though
>it makes for slight syntactical asymmetries, i.e., the use of a
>function pointer looks different that its declaration, and taking
>the address of a function looks different that taking the address
>of an object (other than arrays).

I've thought about the asymmetry between non-member functions and
member functions, and my conclusion was that the asymmetry is
required.  With non-member functions, the '&' when taking the
address is optional, and the '*' when calling a function pointer
is also optional.  With member functions, the '&' when taking the
address is required (though most compilers don't complain), and
the '*' is required (of course).

Consider this template function
   template <class Iter, class Action>
   Action for_each(Iter begin, const Iter end, Action action) {
      while (begin!=end) action(*begin++);
      return action;
   }
If Action is a class MyClass, the the action(...) calls
MyClass::operator()(const T&).
If Action is a non-member function pointer void (*)(const T&),
then the action(...) calls the pointed to function by address.
That is, the action(...) is equivalent to (*action)(...).

Now if C++ required the use of '*' when calling a pointer to a
non-member function, then the above useful template function
wouldn't not work for Action a pointer to a non-member function.
This would really suck.

So we really need the rule that the '*' is optional.  By symmetry,
it follows that the '&' should be optional too.  Though I always
use the '&', as a matter of style.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1999/05/22
Raw View
On 21 May 1999 18:37:28 GMT, Ed Brey <brey@afd.mke.etn.com> wrote:
>Siemel Naran <sbnaran@fermi.ceg.uiuc.edu> wrote in message

>> Irrelevant.  Constant pointers have pretty much the same properties as
>> references.  Example:

>The same arguments apply for pointers/references to functions as for
>pointers/references to variables.  For example, you could apply the above
>argument to question the need for references to variables:

We need references so that we can get: (1) polymorhpism, (2) operator
overloading.  As functions can't be polymorphic (only objects can be
polymorphic), (1) does not apply.  As we can't create and return
functions, (2) does not apply.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Jacob Gladish <gladish@psc.edu>
Date: 1999/05/22
Raw View
This code does not compile with egcs-1.1. I get the following error....

luddite>g++ test.cpp -o test
test.cpp:8: cannot declare references to functions; use pointer to
function instead
luddite>

 I also tried to compile with Sun/Sparc cc and got some strange error
that didn't make much sense to me. It complains during linking about an
unknown type. Can anyone get this compile (with the error commented out
of course)? If anyone has a minute to try this out, please post with the
compiler your using.

#include <stdio.h>

void foo1() { printf( "foo1()\n" ); }

void foo2() { printf( "foo2()\n" ); }

typedef void (*pf)();
typedef void (&rf)();  // line 8

int main()
{
    pf p = foo1;
    rf r = foo1;

    p();
    r();

    p = foo2;
    r = foo2; // error!

    p();
    r();

    return 0;
}
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1999/05/22
Raw View
In article <7i48uv$6ls3@interserv.etn.com>,
Ed Brey <brey@afd.mke.etn.com> wrote:
>The same arguments apply for pointers/references to functions as for
>pointers/references to variables.

But pointers to variables aren't automatically dereferenced when they're
used.  Essentially, a reference is a constant pointer that's automatically
dereferenced.  Since function pointers are also automatically dereferenced,
a constant function pointer is mostly equivalent to a function reference.
The same relationship doesn't exist between constant object pointers and
object references, because of the need to dereference explicitly.

Regarding the original question, one of the main uses of references is to
have an alias, so that when you assign to a reference it actually assigns
to what it refers to (this is particularly useful to provide
call-by-reference functions).  But assignments are only allowed with object
types, not function types, so this use of references is not meaningful with
the latter.

--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/05/23
Raw View
Jacob Gladish <gladish@psc.edu> writes:

>This code does not compile with egcs-1.1. I get the following error....

>luddite>g++ test.cpp -o test
>test.cpp:8: cannot declare references to functions; use pointer to
>function instead
>luddite>

That would be a bug in g++.

> I also tried to compile with Sun/Sparc cc and got some strange error
>that didn't make much sense to me. It complains during linking about an
>unknown type.

If you really used the "cc" command, I'm not surprised. That's
the C compiler, and C does not have references.

If you use the Sun C++ compiler ("CC" command), your code (below)
compiles without complaint. I tried both the 4.2 and 5.0
compilers. Both of them compiled and ran the code.

>#include <stdio.h>

>void foo1() { printf( "foo1()\n" ); }

>void foo2() { printf( "foo2()\n" ); }

>typedef void (*pf)();
>typedef void (&rf)();  // line 8

>int main()
>{
>    pf p = foo1;
>    rf r = foo1;

>    p();
>    r();
>
>    p = foo2;
>    r = foo2; // error!

>    p();
>    r();
>
>    return 0;
>}

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Ed Brey" <brey@afd.mke.etn.com>
Date: 1999/05/21
Raw View
Siemel Naran <sbnaran@fermi.ceg.uiuc.edu> wrote in message
news:slrn7k8ohk.p45.sbnaran@fermi.ceg.uiuc.edu...
>
> On 20 May 1999 18:42:43 GMT, Biju Thomas <b_thomas@ibm.net> wrote:
>
> >A few differences:
>
> Irrelevant.  Constant pointers have pretty much the same properties as
> references.  Example:
>    const int& (*const func)(const int&, const int&)=&std::min<int>;
>    func=&std::max<int>; // error!

The same arguments apply for pointers/references to functions as for
pointers/references to variables.  For example, you could apply the above
argument to question the need for references to variables:

int* const p = &var1;
p = &var2;  // error!

So in general, we don't need references to prevent reassigning p or func.

There is a big benefit of references, however, which is that they cannot be
null or uninitialized.  This simplifies interfaces by eliminating the
possibility of a pointer being set to null or unset when such would not make
any sense.  This argument is just as true for references to functions as it
is for references to variables.



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David R Tribble <dtribble@technologist.com>
Date: 1999/05/21
Raw View
Jacob Gladish wrote:
>
> andrewalex@hotmail.com wrote:
>> One can have pointers to functions and references to functions. This
>> has a curious twist, given that the compiler applies operator* to
>> pointers to functions anyway:
>>
>> int f();
>> int (& rf)() = f;
>> int (* pf)() = f;
>> rf();
>> pf();
>
> I hope this isn't off the group topic, but what is the difference
> between a refernece to a function and pointer to a function, and does
> it really matter which one you use?

One difference is that the code above is actually more "correctly"
written as:

    int (& rf) = f;
    int (* pf) = &f;
    rf();
    (*pf)();

It's an semantic shorthand of C++ (and C) that lets you omit the '&'
and '*' operators when dealing with function pointers, even though
it makes for slight syntactical asymmetries, i.e., the use of a
function pointer looks different that its declaration, and taking
the address of a function looks different that taking the address
of an object (other than arrays).

One old rule states that it's generally better to use a pointer
than a reference if you intend to modify the thing it's pointing to.
This rule doesn't apply here, of course, since the thing we're
pointing to is a function, which can't be altered anyway.
That being the case, I suppose it's a little more "pure" to
prefer function references over function pointers.

On the other hand, the primary difference between pointers and
references is their modifiability; you can't change (reseat) a
reference to refer to another object/function once it's been
initialized, but you can always change a (non-const) pointer.
The following illustrates this difference:

    rf = g;     // illegal
    pf = g;     // legal
    pf = NULL;  // legal

So the question of preferring references or pointers would seem to
boil down to a matter of whether you intend to change the pointer;
if you don't intend to, then it seems to boil down to purely to
a matter of style.

-- David R. Tribble, dtribble@technologist.com --


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: andrewalex@hotmail.com
Date: 1999/05/19
Raw View
I guess this has been discussed already. Please point me to the
appropriate thread if this has already been hashed to death.

One can have pointers to functions and references to functions. This has
a curious twist, given that the compiler applies operator* to pointers
to functions anyway:

int f();
int (& rf)() = f;
int (* pf)() = f;
rf();
pf();

Okay, now the question is: is there a serious reason for not allowing
references to member functions? This introduces a strange asymmetry
here.

Andrei


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1999/05/19
Raw View
On 19 May 1999 19:16:12 GMT, andrewalex@hotmail.com

>Okay, now the question is: is there a serious reason for not allowing
>references to member functions? This introduces a strange asymmetry
>here.

Nope.  The real question is there a serious reason to even allow a
reference to a non-member function :).

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Jacob Gladish <gladish@psc.edu>
Date: 1999/05/20
Raw View
andrewalex@hotmail.com wrote:
>
> I guess this has been discussed already. Please point me to the
> appropriate thread if this has already been hashed to death.
>
> One can have pointers to functions and references to functions. This has
> a curious twist, given that the compiler applies operator* to pointers
> to functions anyway:
>
> int f();
> int (& rf)() = f;
> int (* pf)() = f;
> rf();
> pf();
>

 I hope this isn't off the group topic, but what is the difference
between a
refernece to a function and pointer to a function, and does it really
matter which one you use?

jake
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Biju Thomas <b_thomas@ibm.net>
Date: 1999/05/20
Raw View
Jacob Gladish wrote:
>
>         I hope this isn't off the group topic, but what is the difference
> between a
> refernece to a function and pointer to a function, and does it really
> matter which one you use?
>

A few differences:

1. References to functions has to be initialized in the declaration,
but, you don't have to initialize pointers to functions.

2. References to functions are 'const' references implicitly, so, they
can't be assigned later. (If C++ allowed assigning to references to
functions, it will modify code during run-time. Enabling such wonderful
things are dangerous.) This is not the case with pointers to functions.

So, in a particular case, references to functions may be better suited
than pointers to functions, or, vice-versa.

--
Biju Thomas


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1999/05/20
Raw View
On 20 May 1999 18:42:43 GMT, Biju Thomas <b_thomas@ibm.net> wrote:

>A few differences:

Irrelevant.  Constant pointers have pretty much the same properties as
references.  Example:
   const int& (*const func)(const int&, const int&)=&std::min<int>;
   func=&std::max<int>; // error!

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Andrei Alexandrescu <andrewalex@hotmail.com>
Date: 1999/05/21
Raw View
In article <37432C85.B70FF9A@psc.edu>,
  Jacob Gladish <gladish@psc.edu> wrote:
>  I hope this isn't off the group topic

I guess it's not.

>, but what is the
difference
> between a
> refernece to a function and pointer to a function, and does it really
> matter which one you use?

I think the only difference that remains is that you cannot rebind the
reference:

void f();

typedef void (*pf)();
typedef void (&rf)();

int main()
{
 pf p;
 rf r = f;
 p = f;
 r = f; // error!
 return 0;
}

Andrei


--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1999/05/21
Raw View
On 21 May 99 15:22:46 GMT, Andrei Alexandrescu <andrewalex@hotmail.com> wrote:

>I think the only difference that remains is that you cannot rebind the
>reference:

Then use a constant pointer -- that is, a *const.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]