1

I know that a function definition can't be done through typedef. For example:

typedef int f_t(int x, int y);

f_t fsum
{
    int sum;
    sum = x + y;
    return sum;
}

But I can't find the provision which forbids this definition in C99. Which provisions are related and how they forbid this definition?

junwanghe
  • 187
  • 1
  • 7
  • Related: [Why can't a typedef of a function be used to define a function?](http://stackoverflow.com/q/17848983/1009479) – Yu Hao Oct 23 '13 at 08:04

2 Answers2

4

This is immediately given in the clause describing function definitions:

6.9.1 Function definitions

Constraints

2 - The identifier declared in a function definition (which is the name of the function) shall have a function type, as specified by the declarator portion of the function definition.141)

141) The intent is that the type category in a function definition cannot be inherited from a typedef [...]

The way this works is that under declarators (6.7.5), the only function declarators (6.7.5.3) are those with parentheses after the identifier:

T D( parameter-type-list )
T D( identifier-listopt )

So, if the function is defined via a typedef then it does not have the syntactic form of a function declarator and so is invalid by 6.9.1p2.

It may help to consider the syntactic breakdown of an attempted typedef function definition:

typedef int F(void);

F f {}
| | ^^-- compound-statement
| ^-- declarator
^-- declaration-specifiers

Note that the typedef type F is part of the declaration-specifiers and not part of the declarator, and so f (the declarator portion) does not have a function type.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
0

Since you're asking about the intent of forbidding typedefs from being used in a function definition (in some comments), the C99 rationale has this to say about it:

An argument list must be explicitly present in the declarator; it cannot be inherited from a typedef (see §6.7.5.3). That is to say, given the definition:

typedef int p(int q, int r); 

the following fragment is invalid:

p funk  // weird 
{ return q + r ; }

So it seems that the function defintion-by-typedef was disallowed because the definition didn't look enough like a function in that case.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760