Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a parameter forward declaration?

I thought I knew C syntax quite well, until I tried to compile the following code:

void f(int i; double x) { } 

I expected the compiler to trip, and it did, but I don't get the error message:

test.c:1:14: error: parameter ‘i’ has just a forward declaration 

I then tried

void fun(int i; i) { } 

which fails with

test.c:1:17: error: expected declaration specifiers or ‘...’ before ‘i’ 

and finally

void fun(int i; int i) { } 

which, much to my surprise, succeeds!

I've never seen this syntax in real-world C code. What is its use?

like image 694
Fred Foo Avatar asked Jul 21 '13 10:07

Fred Foo


People also ask

What is meant by forward declaration?

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.

What can be forward declared?

The main rule is that you can only forward-declare classes whose memory layout (and thus member functions and data members) do not need to be known in the file you forward-declare it. This would rule out base classes and anything but classes used via references and pointers. Almost.

Should I use forward declaration or include?

As the name itself implies, forward declaration is just a Declaration and not a definition. So, you will declare saying the compiler that it is a class and I just declaring it here and will provide you the definition when am gonna use it. So, normally you forward declare in the Header file and #include in the .

How do you fix a forward declaration in C++?

To solve this, you can forward-declare the parts you need in one of the files and leave the #include out of that file. Hmm... the declaration of Car is required here as Wheel has a pointer to a Car , but Car. h can't be included here as it would result in a compiler error.


1 Answers

This form of function definition:

void fun(int i; int i) { } 

uses a GNU C extension called the parameter forward declaration feature.

http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

This feature allows you to have parameter forward declarations before the actual list of parameters. This can be used for example for functions with variable length array parameters to declare a size parameter after the variable length array parameter.

For example:

// valid, len parameter is used after its declaration  void foo(int len, char data[len][len]) {}    // not valid, len parameter is used before its declaration void foo(char data[len][len], int len) {}  // valid in GNU C, there is a forward declaration of len parameter // Note: foo is also function with two parameters void foo(int len; char data[len][len], int len) {}   

In the OP example,

void fun(int i; int i) {} 

the forward parameter declaration does not serve any purpose as it is not used in any of the actual parameters and the fun function definition is actually equivalent to:

void fun(int i) {} 

Note this is a GNU C extension and it is not C. Compiling with gccand -std=c99 -pedantic would give the expected diagnostic:

warning: ISO C forbids forward parameter declarations [-pedantic]

like image 172
ouah Avatar answered Sep 19 '22 23:09

ouah