Topic: References to functions


Author: Brett.Peterson@p99.f426.n114.z1.fidonet.org (Brett Peterson)
Date: 26 Sep 94 03:59:42 -0800
Raw View
From: brettp@mahjongg.rchland.ibm.com (Brett Peterson)
Newsgroups: comp.std.c++

In article <1994Sep26.095144.20949@fmrco.uucp>, dcb@lovat.fmrco.com (David
Binderman) writes:
|> Is the following code legal ?
|> #include <iostream.h>
|>
|> void
|> f() {
|>  cout << "Hello World\n";
|> };
|>
|> void (&rf)() = f;
|>
|> int
|> main()
|> {
|>  rf();
|>  return 0;
|> }
|>
|> I cannot see anything in the ARM about this "feature", and the compilers
|> are in disagreement.

I would say yes since ARM section 3.6.2 and draft standard section 3.8.2
indicate that:

"There is a conceptually infinite number of derived types constructed from
the fundamental types in
the following ways:
...
references to objects OR FUNCTIONS (emphasis mine) of a given type"

Brett




Author: dcb@lovat.fmrco.com (David Binderman)
Date: Mon, 26 Sep 1994 09:51:44 GMT
Raw View
Hello there,

Is the following code legal ?


#include <iostream.h>

void
f() {
 cout << "Hello World\n";
};

void (&rf)() = f;

int
main()
{
 rf();
 return 0;
}

I cannot see anything in the ARM about this "feature", and the compilers
are in disagreement.

Regards

David C Binderman BSc MSc      +44 171 975 4723         dcb@lovat.fmrco.com
Real computers have a terminal interface (preferably vt100). Toy computers have GUI





Author: brettp@mahjongg.rchland.ibm.com (Brett Peterson)
Date: 26 Sep 1994 11:59:42 GMT
Raw View
In article <1994Sep26.095144.20949@fmrco.uucp>, dcb@lovat.fmrco.com (David Binderman) writes:
|> Is the following code legal ?
|> #include <iostream.h>
|>
|> void
|> f() {
|>  cout << "Hello World\n";
|> };
|>
|> void (&rf)() = f;
|>
|> int
|> main()
|> {
|>  rf();
|>  return 0;
|> }
|>
|> I cannot see anything in the ARM about this "feature", and the compilers
|> are in disagreement.

I would say yes since ARM section 3.6.2 and draft standard section 3.8.2 indicate that:

"There is a conceptually infinite number of derived types constructed from the fundamental types in
the following ways:
...
references to objects OR FUNCTIONS (emphasis mine) of a given type"

Brett




Author: shoe@objectSpace.com (Brett L. Schuchert)
Date: Mon, 26 Sep 1994 18:51:35 GMT
Raw View
>void
>f() {
> cout << "Hello World\n";
>};
>
>void (&rf)() = f;

I'd guess you'd need to write something like (although this won't work):

 void (const * & rf)() = f;

The () operator on a function pointer calls the function. Given a
reference to a function, you'd have to write:
 (&rf)()

But this assumes your original assignment was correct, which it
wasn't. You tried to assign f, a pointer to a function taking no
parameters and returning void, to rf, a reference to a function
taking no parameters and returning void. What is a reference to
a function (I'm not sure). I know what a pointer to a function
is, however, and f is a pointer to a function.

You might be able to make a reference to a pointer to a function
but I'd guess that'd have to be a const reference since f
should be a constant pointer to the funciton (just like the name
of an array is a constant pointer to the type in the array).

So the following _will_ work:

void (*rf)() = f;
...
rf();

Stroustrup would prefer you write:
 void (*rf)() = &f;

Under C++, both work. Under ANSI C, a compiler would warn you about
a superfluous & with the function pointer (actually so will some C++
compilers).

But I'm rambling.

To get your original version type consistent, you'd have to write:

 void (&rf)() = *f;

Will this work? I tried on a few compilers and it didn't.

Hope this helps (although I'm not sure that it did).

shoe




Author: dcb@lovat.fmrco.com (David Binderman)
Date: Tue, 27 Sep 1994 12:43:42 GMT
Raw View
I wrote:
>I cannot see anything in the ARM about this "feature", and the compilers
>are in disagreement.

The point is that this feature is described in one line of "Derived Types"
section, not "References" section where I expected it.

Ron wrote:
>Which compilers?  The serious commercial ones I have agree that it is
>perfectly valid code.

Wouldn't like to say, since its a very good compiler and it deserves lots of sales. However, I will say that it's not from New Jersey, and it cost money.

The vendor, even though the dev. gruppe are probably reading this anyway,
has been informed.

>(GNU doesn't yet know that references to functions are allowed, but that's
>hardly the only GNU bug, now is it?)

Agreed. I applaud the persistence of the G++ folks. They've got years of work
ahead of them removing hundreds of bugs.

Regards

David C Binderman BSc MSc      +44 171 975 4723         dcb@lovat.fmrco.com
Academy Programming Ltd - the C++ on UNIX specialists.





Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Tue, 27 Sep 1994 08:53:41 GMT
Raw View
In article <1994Sep26.095144.20949@fmrco.uucp> dcb@lovat.fmrco.com writes:
>Hello there,
>
>Is the following code legal ?
>
>
>#include <iostream.h>
>
>void
>f() {
> cout << "Hello World\n";
>};
>
>void (&rf)() = f;
>
>int
>main()
>{
> rf();
> return 0;
>}
>
>I cannot see anything in the ARM about this "feature", and the compilers
>are in disagreement.

Which compilers?  The serious commercial ones I have agree that it is
perfectly valid code.

(GNU doesn't yet know that references to functions are allowed, but that's
hardly the only GNU bug, now is it?)

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -