public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* 'curiously recursive template' example
@ 1999-06-30 23:07 B. K. Oxley (binkley)@Home
1999-07-01 21:57 ` Alexandre Oliva
0 siblings, 1 reply; 2+ messages in thread
From: B. K. Oxley (binkley)@Home @ 1999-06-30 23:07 UTC (permalink / raw)
To: egcs-bugs
To cut to the chase, see .ii file and compiler output at bottom. :-)
--binkley
>From this newsposting to comp.lang.c++.moderated:
Path:
ndnws01.ne.mediaone.net!chnws05.ne.mediaone.net!24.128.1.91!chnws02.mediaone.net!192.148.253.68!netnews.com!news-peer1.sprintlink.net!news-in-east1.sprintlink.net!news.sprintlink.net!rpi!not-for-mail
From: herwin@gmu.edu (Harry Erwin)
Newsgroups: comp.lang.c++.moderated
Subject: Re: Guru of the Week #57: Recursive Declarations
Date: 28 Jun 1999 23:04:34 -0400
Organization: HDE Associates
Lines: 64
Sender: cppmods@netlab.cs.rpi.edu
Approved: jep@eagle.lhup.edu
Message-ID:
<1du45k3.1a7dt3t1ous0qiN@user-2iveigp.dialup.mindspring.com>
References: <377768e7.19229607@nntp.netcom.ca>
NNTP-Posting-Host: netlab.cs.rpi.edu
X-Original-Date: Mon, 28 Jun 1999 15:00:16 -0400
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
Xref: chnws05.ne.mediaone.net
comp.lang.c++.moderated:32935
Herb Sutter <hsutter@peerdirect.com> wrote:
>
> JG Question
> -----------
>
> 1. What is a pointer to function? How can it be used?
It's an address to a function that can be dereferenced and then used to
call the function. It's very useful in operating systems for building
dispatch tables and to store the address of completion routines for
'later'. It's similarly useful in event-stepped and combined simulation
packages. It's now obsolete, since function objects (instances of a
class with operator()() defined) can be used for the same purpose with a
good deal more type safety.
>
>
> Guru Questions
> --------------
>
> 2. Assume that it is possible to write a function that
> can return a pointer to itself. Then that function
> could equally well return a pointer to any function
> with the same signature as itself. When might this
> be useful? Explain.
Think of an encapsulated dispatch table, with the class instance
containing a specific dispatch table. Calling some member function
returns a pointer to the currently scheduled operation. I did something
similar in my dissertation program--the event queue was implemented as
an STL priority queue, with the events pointers to various function
objects that have been scheduled for execution at various times. Since
this mixes generic programming and OOP, I have to use a wrapper and a
clone() member function (see Mumit Khan's wonderful web page) to store
events of varying types in the event queue without slicing when the
event pointer is copied. It works very nicely and gives me something
approximating the event queue management capabilities of Simula or
SmallTalk.
>
> 3. Is it possible to write a function f() that returns
> a pointer to itself? It should be usable in the
> following natural way:
>
> // FuncPtr is a typedef for a pointer to a function
> // with the same signature as f()
>
> FuncPtr p = f(); // executes f()
> (*p)(); // executes f()
>
> If it is possible, demonstrate how. If it is not
> possible, explain why.
It's hard or impossible to do for a real function, but it's trivial for
a function object. Just return this.
--
Harry Erwin, < http://mason.gmu.edu/~herwin >, Sr SW Analyst,
PhD cand (informatics/computational sci) modeling how bats
echolocate (def Sept), and Adj Prof of CS (data struct/adv C++).
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this!
]Path:
ndnws01.ne.mediaone.net!chnws05.ne.mediaone.net!24.128.1.91!chnws02.mediaone.net!192.148.253.68!netnews.com!news-peer1.sprintlink.net!news-in-east1.sprintlink.net!news.sprintlink.net!rpi!not-for-mail
From:
herwin@gmu.edu (Harry Erwin)
Newsgroups:
comp.lang.c++.moderated
Subject:
Re: Guru of the Week #57: Recursive Declarations
Date:
28 Jun 1999 23:04:34 -0400
Organization:
HDE Associates
Lines:
64
Sender:
cppmods@netlab.cs.rpi.edu
Approved:
jep@eagle.lhup.edu
Message-ID:
<1du45k3.1a7dt3t1ous0qiN@user-2iveigp.dialup.mindspring.com>
References:
<377768e7.19229607@nntp.netcom.ca>
NNTP-Posting-Host:
netlab.cs.rpi.edu
X-Original-Date:
Mon, 28 Jun 1999 15:00:16 -0400
X-Submission-Address:
c++-submit@netlab.cs.rpi.edu
X-Auth:
PGPMoose V1.1 PGP comp.lang.c++.moderated
iQBVAwUAN3g3v0HMCo9UcraBAQFt1QH/XXQe/ei33pmKd6OVVvyuRg7UEOJwV6cr
5lzt9UXWzHvQk6sRR4ODthK4RaFovqlkLurQBscKSAfnQ64+gAkhrg== =9LEP
Xref:
chnws05.ne.mediaone.net
comp.lang.c++.moderated:32935
Herb Sutter <hsutter@peerdirect.com> wrote:
>
> JG Question
> -----------
>
> 1. What is a pointer to function? How can it be used?
It's an address to a function that can be dereferenced and then used to
call the function. It's very useful in operating systems for building
dispatch tables and to store the address of completion routines for
'later'. It's similarly useful in event-stepped and combined simulation
packages. It's now obsolete, since function objects (instances of a
class with operator()() defined) can be used for the same purpose with a
good deal more type safety.
>
>
> Guru Questions
> --------------
>
> 2. Assume that it is possible to write a function that
> can return a pointer to itself. Then that function
> could equally well return a pointer to any function
> with the same signature as itself. When might this
> be useful? Explain.
Think of an encapsulated dispatch table, with the class instance
containing a specific dispatch table. Calling some member function
returns a pointer to the currently scheduled operation. I did something
similar in my dissertation program--the event queue was implemented as
an STL priority queue, with the events pointers to various function
objects that have been scheduled for execution at various times. Since
this mixes generic programming and OOP, I have to use a wrapper and a
clone() member function (see Mumit Khan's wonderful web page) to store
events of varying types in the event queue without slicing when the
event pointer is copied. It works very nicely and gives me something
approximating the event queue management capabilities of Simula or
SmallTalk.
>
> 3. Is it possible to write a function f() that returns
> a pointer to itself? It should be usable in the
> following natural way:
>
> // FuncPtr is a typedef for a pointer to a function
> // with the same signature as f()
>
> FuncPtr p = f(); // executes f()
> (*p)(); // executes f()
>
> If it is possible, demonstrate how. If it is not
> possible, explain why.
It's hard or impossible to do for a real function, but it's trivial for
a function object. Just return this.
--
Harry Erwin, < http://mason.gmu.edu/~herwin >, Sr SW Analyst,
PhD cand (informatics/computational sci) modeling how bats
echolocate (def Sept), and Adj Prof of CS (data struct/adv C++).
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
main.ii:
# 1 "main.cc"
template<class T>
struct FuncPtr_helper
{ typedef typename T::f (*f) ( ); };
struct FuncPtr_typedef
: FuncPtr_helper<FuncPtr_typedef>
{ }
typedef FuncPtr_typedef::f FuncPtr;
FuncPtr f ( )
{
return f;
}
int main ( )
{
FuncPtr p = f ( );
(*p) ( );
return 0;
}
output:
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
/usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/cpp -lang-c++ -v -undef
-D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -D__GNUC_MINOR__=91 -D__ELF__
-Dunix -Di386 -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__
-D__i386__ -D__linux__ -D__unix -D__i386 -D__linux -Asystem(posix)
-D__EXCEPTIONS -Asystem(unix) -Acpu(i386) -Amachine(i386) -Di386
-D__i386 -D__i386__ main.cc main.ii
GNU CPP version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release) (i386
Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
/usr/include/g++-2
/usr/local/include
/usr/i386-redhat-linux/include
/usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include
/usr/include
End of search list.
/usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/cc1plus main.ii -quiet
-dumpbase main.cc -version -o main.s
GNU C++ version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
(i386-redhat-linux) compiled by GNU C version egcs-2.91.66
19990314/Linux (egcs-1.1.2 release).
main.cc: In instantiation of `FuncPtr_helper<FuncPtr_typedef>':
main.cc:7: instantiated from here
main.cc:7: Internal compiler error.
main.cc:7: Please submit a full bug report to
`egcs-bugs@egcs.cygnus.com'.
main.cc:7: See <URL: http://egcs.cygnus.com/faq.html#bugreport > for
details.
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: 'curiously recursive template' example
1999-06-30 23:07 'curiously recursive template' example B. K. Oxley (binkley)@Home
@ 1999-07-01 21:57 ` Alexandre Oliva
0 siblings, 0 replies; 2+ messages in thread
From: Alexandre Oliva @ 1999-07-01 21:57 UTC (permalink / raw)
To: binkley; +Cc: egcs-bugs
On Jun 29, 1999, "B. K. Oxley (binkley) at Home" <binkley@bigfoot.com> wrote:
> template<class T> struct FuncPtr_helper
> { typedef typename T::f (*f) ( ); };
> struct FuncPtr_typedef : FuncPtr_helper<FuncPtr_typedef> { }
> main.cc:7: Internal compiler error.
Thanks for your bug report. The upcoming gcc 2.95 will report:
main.cc: In instantiation of `FuncPtr_helper<FuncPtr_typedef>':
main.cc:7: instantiated from here
main.cc:3: invalid use of undefined type `struct FuncPtr_typedef'
main.cc:6: forward declaration of `struct FuncPtr_typedef'
main.cc:9: syntax error before `;'
And a few other errors caused by these. In fact, because
FuncPtr_helper requires its argument to be a complete type,
FuncPtr_helper<FuncPtr_typedef> can't be instantiated before
FuncPtr_typedef is fully defined, so the template instantiation can't
be used as a base class.
--
Alexandre Oliva http://www.dcc.unicamp.br/~oliva IC-Unicamp, Bra[sz]il
{oliva,Alexandre.Oliva}@dcc.unicamp.br aoliva@{acm.org,computer.org}
oliva@{gnu.org,kaffe.org,{egcs,sourceware}.cygnus.com,samba.org}
*** E-mail about software projects will be forwarded to mailing lists
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1999-07-01 21:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-06-30 23:07 'curiously recursive template' example B. K. Oxley (binkley)@Home
1999-07-01 21:57 ` Alexandre Oliva
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).