[comp.std.c] typedefing functions

enag@ifi.uio.no (Erik Naggum) (02/05/91)

In article <1991Feb4.053613.3758@watmath.waterloo.edu>, David Tanguay writes:

> Our compiler had a neat bug with this. It would accept, e.g., 

> 	typedef int f( int a ) { return a+1; }

> "f" would be a type thereafter in that file, but it was also created
> as an external visible only outside that file.  Syntactically typedef
> is the same as static or extern.

> Is the above typedef valid in ANSI-C (as a typedef)?  It's not a
> valid function declaration (has to be static or extern), but I can't
> recall anything making it an invalid typedef.

No.  You cannot achieve function type through typedef.  I remember
this phrase from the standard, but can't find it right now.  Note that
you can say

	typedef int f (int a, char *b);

and later say

	f foo, bar, zot;

and

	f *functab[3] = { foo, bar, zot, };

The functions would be declared

	f foo
	{ ... }

	f bar
	{ ... }

	f zot
	{ ... }

This will, naturally, make programs like ctags somewhat less likely to
find the functions, but it's a very good way to provide consistent
interface to functions that are referenced only through function
pointers.

--
[Erik Naggum]					     <enag@ifi.uio.no>
Naggum Software, Oslo, Norway			   <erik@naggum.uu.no>

henry@zoo.toronto.edu (Henry Spencer) (02/05/91)

In article <1991Feb4.053613.3758@watmath.waterloo.edu> datangua@watmath.waterloo.edu (David Tanguay) writes:
>	typedef int f( int a ) { return a+1; }
>
>Is the above typedef valid in ANSI-C (as a typedef)? It's not a valid
>function declaration (has to be static or extern), but I can't recall
>anything making it an invalid typedef.

It's not a valid typedef because that `{ return a+1; }' cannot appear in
a declaration of any kind.  The presence of the function body makes this
a function definition, not a declaration, and `typedef' is not legal in
a function definition.
-- 
"Maybe we should tell the truth?"      | Henry Spencer at U of Toronto Zoology
"Surely we aren't that desperate yet." |  henry@zoo.toronto.edu   utzoo!henry

henry@zoo.toronto.edu (Henry Spencer) (02/05/91)

In article <ENAG.91Feb4192806@holmenkollen.ifi.uio.no> enag@ifi.uio.no (Erik Naggum) writes:
>you can say
>	typedef int f (int a, char *b);
>and later say
>	f foo, bar, zot;
>and
>	f *functab[3] = { foo, bar, zot, };

Correct so far.

>The functions would be declared
>	f foo
>	{ ... }

Nope, wrong.  The function definition itself must contain an explicit
function declarator; it cannot inherit its "functionness" from a typedef.
See Constraints in 3.7.1.
-- 
"Maybe we should tell the truth?"      | Henry Spencer at U of Toronto Zoology
"Surely we aren't that desperate yet." |  henry@zoo.toronto.edu   utzoo!henry

enag@ifi.uio.no (Erik Naggum) (02/05/91)

In article <ENAG.91Feb4192806@holmenkollen.ifi.uio.no>, Erik Naggum writes:

> The functions would be declared
> 
> 	f foo
> 	{ ... }

I have been informed that this is entirely bogus.

--
[Erik Naggum]					     <enag@ifi.uio.no>
Naggum Software, Oslo, Norway			   <erik@naggum.uu.no>

kooijman@duteca (Richard Kooijman) (02/06/91)

henry@zoo.toronto.edu (Henry Spencer) writes:

>In article <ENAG.91Feb4192806@holmenkollen.ifi.uio.no> enag@ifi.uio.no (Erik Naggum) writes:
>>you can say
>>	typedef int f (int a, char *b);
>>and later say
>>	f foo, bar, zot;
>>and
>>	f *functab[3] = { foo, bar, zot, };

>Correct so far.

>>The functions would be declared
>>	f foo
>>	{ ... }

>Nope, wrong.  The function definition itself must contain an explicit
>function declarator; it cannot inherit its "functionness" from a typedef.
>See Constraints in 3.7.1.

But what is the reason? I think it severely limits the use of
typedef'ing functions, which could be otherwise very useful:

typedef void cntrlc_handler(void);

main()
{
	cntrlc_handler c_handler;

	signal(SIGINT, c_handler);
}

It is a shame to be forced to define c_handler again:


void c_handler(void)
{
   ...
}





Richard.

avery@netcom.COM (Avery Colter) (02/08/91)

datangua@watmath.waterloo.edu (David Tanguay) writes:

>Our compiler had a neat bug with this. It would accept, e.g., 

>	typedef int f( int a ) { return a+1; }

Hmmmmm......

Ya meeeeeeeeen.......

I could just see this:

f foo;

foo is a function with an int parameter returning an int,
AND has the statement body {return a+1;}!

Probably not legal, but hey, I've been wrong before this week. :P

-- 
Avery Ray Colter    {apple|claris}!netcom!avery  {decwrl|mips|sgi}!btr!elfcat
(415) 839-4567   "I feel love has got to come on and I want it:
                  Something big and lovely!"         - The B-52s, "Channel Z"