becher@armada.UUCP (Jonathan D. Becher) (10/12/88)
The following program compiles with gcc but not with g++. It seems as if g++ doesn't understand formal parameters that are pointers to functions. If anyone can think of a way around this bug (or explain to me why it isn't one), please let me know. I can't live without pointers to functions :-> Program: #include <stdio.h> doSomething() { printf("I did\n"); } doThis(int (*f)()) { f(); } main() { doThis(doSomething); } Compilation: g++ version 1.25.0 /usr/local/lib/gcc-cpp+ -v -undef -D__GNU__ -D__GNUG__ -Dvax -Dunix t.cc /tmp/cc011037.cpp /usr/local/lib/gcc-c++ /tmp/cc011037.cpp -quiet -dumpbase t.cc -noreg -version -o /tmp/cc011037.s GNU C++ version 1.25.0 (vax) compiled by GNU C version 1.25. t.cc:9: undeclared variable `f' (first use here) ===== Jon Becher becher@cs.duke.edu -OR- argosy!becher@decwrl.dec.com
becher@armada.UUCP (Jonathan D. Becher) (10/12/88)
In article <68@armada.UUCP>, I write: > It seems as if g++ doesn't understand formal parameters that > are pointers to functions. In my previous posting, you can get around this error by using a typedef. For some reason, this: > doThis(int (*f)()) > { > f(); > } doesn't work but this does: typedef int (*ptrFunc)(); doThis(ptrFunc f) { f(); } In any event, it's still a bug. ===== Jon Becher becher@cs.duke.edu -OR- argosy!becher@decwrl.dec.com "I go to parties, some times until four. It's hard to leave when you can't find the door."
tiemann@LURCH.STANFORD.EDU (Michael Tiemann) (10/12/88)
Date: 11 Oct 88 21:45:49 GMT
From: armada!becher@decwrl.dec.com (Jonathan D. Becher)
Organization: I have no organization (look at my desk)
Sender: bug-g++-request@prep.ai.mit.edu
The following program compiles with gcc but not with g++.
It seems as if g++ doesn't understand formal parameters that
are pointers to functions.
If anyone can think of a way around this bug (or explain to me
why it isn't one), please let me know.
I can't live without pointers to functions :->
From your previous posting, it is clear that you already know that
using a typedef solves this problem. By using a typedef, you save
compilation time (less declarator structure to grok). If, however,
you are somehow alergic to using typedefs, one thing you can do is this:
doThis(auto int (*f)())
{
f();
}
This will (or should) then compile. This is a bug in the C++
language. I wish that the designers of C++ knew enough about LALR
parsing to have made their language LALR-parsable; unfortunately, they
did not. I consider the AT&T solution (which also doesn't always work
for pointers to functions in parameter positions) of using heuristics
to help try to guide the parser unacceptable, and certainly something
which I would want to distribute, freely or otherwise.
Michael