I've defined:
typedef int FunkyFunc(int x);
Now, I would like to be able to use this typedef in the definition of functions of type FunkyFunc
, e.g.
FunkyFunc f {
return 2*x;
}
or
FunkyFunc f(int x) {
return 2*x;
}
or
FunkyFunc f(x) {
return 2*x;
}
can I do something similar to any of the above? None of them seems to compile.
C Language Typedef Typedef for Function Pointers This becomes more apparent when function pointers are used in more complex situations, such as arguments to functions. Likewise functions can return function pointers and again, the use of a typedef can make the syntax simpler when doing so.
Step 1: Defining a typedef A typedef can be used to specify a function signature that we want specific functions to match. A function signature is defined by a function's parameters (including their types). The return type is not a part of the function signature. Its syntax is as follows.
The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.
typedef void (*MCB)(void); This is one of the areas where there is a significant difference between C, which does not - yet - require all functions to be prototyped before being defined or used, and C++, which does.
This is ill-formed in C++ from the draft standard section 8.3.5
Functions paragraph 10 says:
A typedef of function type may be used to declare a function but shall not be used to define a function (8.4). [ Example:
typedef void F(); F fv; // OK: equivalent to void fv(); F fv { } // ill-formed void fv() { } // OK: definition of fv
—end example ][...]
We can see in C this is also specifically forbidden from the C99 draft standard section 6.9.1
Function definitions says:
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)
and footnote 141
says:
The intent is that the type category in a function definition cannot be inherited from a typedef:
and has the following example:
typedef int F(void); // type F is ‘‘function with no parameters
// returning int’’
F f, g; // fand g both have type compatible with F
F f { /* ... */ } // WRONG: syntax/constraint error
[...]
C11 says the same things except the footnote is 162
.
No. You can use the typedef to declare a function:
FunkyFunc f;
but a function definition must be written with a function-style declarator.
Note: This is certainly the case in C++, and I'm fairly sure C is the same in this regard; but it would be better if you chose a single language to ask about, since there can be significant differences between C and C++ even where you might imagine they'd be the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With