www.digitalmars.com         C & C++   DMDScript  

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

reply Stephen Zamoscianyk <Stephen_member pathlink.com> writes:
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.


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

#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);

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);

Jul 30 2003
parent "Walter" <walter digitalmars.com> writes:
I suggest using the extra typedef for now. I'll add this to the todo list.
Jul 31 2003