www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - Puzzling Digital Mars C++ Behavior (Repost)

↑ ↓ ← Stephen Zamoscianyk <Stephen_member pathlink.com> writes:
Hello:
This is a repost of my initial inquiry. My apologies, as I discovered the sample
program was not included in the original post. Again thanks for your attention.

-------------------------------------------------------------
Hello:

I discovered a behavior of the Digital Mars compiler that has me puzzled.
The small program reproduced below compiles without error as a .c program
but produces an error when compiled as a .cpp program, namely:

SC -cpp -r -Ae -Ar -Aa -mx -f -S -s -3 -a4 -c   -oFPTR2.obj FPTR2.cpp
int (*  t(int ix))(int *);
^
FPTR2.cpp(9) : Error: '(' expected following simple type name

The startling thing is that other compilers like DJ Delorie's DJGPP (ver 2.81),
Borland's BC++4.5 (bcc -ml -3 -k -N fptr2.cpp), and even (alas) Microsoft's
Visual C++ 4 compile this .cpp code without a complaint. I would be immensely
appreciative if you would clarify if this is some new C++ language behavior or
if this is some kind of bug.

Incidentally, replacing the declaration for the t-function with:
T  t(int ix);   //--FORWARD REFERENCE function prototype
where T is defined as:  typedef  int (*T)(int *k);
makes all the Digital Mars errors go away...

Thanks in advance for any assistance,

Stephen Zamoscianyk
byf9vh cs.com


----fptr2.cpp----
#include <stdio.h>
#include <stdlib.h>

int main(void)
{ int kvectr[7] = { 11, 22, 33, 44, 55, 66, 77 } ,
k = 12, ians1, ians2;

//--FORWARD REFERENCE function prototype
int (*  t(int ix))(int *);

ians1 = t(1)(&k);       //--ians1 = 12 + 3
ians2 = t(2)(kvectr);   //--ians2 = kvectr[4] = 55

printf("ians1 = %d   ians2 = %d \n\n", ians1, ians2);
return(0);
}


int g(int *ip)
{ return(*ip + 3); }

int h(int *ip)
{ return(ip[4]); }

//--function t(int ix) returns ptr to a function
//--having form:  int F(int *k)  like g, h above
int (* t(int ix))(int *)
{ switch(ix)
{ case 1:  return(g);
case 2:  return(h);
default:
break;
}

return(NULL);
}
Jul 30 2003
↑ ↓ → "Walter" <walter digitalmars.com> writes:
I suggest using the extra typedef for now. I'll add this to the todo list.
Jul 31 2003