Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unusual C++ function declaration

There are a few legal ways which can we declare a function in C++.

Some of the legal ways are:

void function ();
void function (void);
dataType function (dataType);

and so on...

Recently, I came across a function declaration as such:

void (function) ();  //Take note of the braces around the function name

I have never seen somehting like this before, when I tested it in a C++ compiler, it runs without any warning or compilation errors.

My question is: Why is void (function) (); a legal way to decalre a function prototype? Is there any special meaning to declare a function in this way? Or does it just work normally like any other function declaration?

like image 879
user3437460 Avatar asked Apr 29 '14 10:04

user3437460


People also ask

How do you declare a function in C?

Function declaration in C always ends with a semicolon. By default the return type of a function is integer(int) data type. Function declaration is also known as function prototype. Name of parameters are not compulsory in function declaration only their type is required. Hence following declaration is also valid. int getSum(int, int);

Do every function have to be explicitly declared in C?

Every function must be explicitly declared before it can be called. In C90, if a function is called without an explicit declaration, the compiler is going to complain about the implicit declaration.

What is implicit declaration of function in C?

Implicit declaration of the function is not allowed in C programming. Every function must be explicitly declared before it can be called. In C90, if a function is called without an explicit declaration, the compiler is going to complain about the implicit declaration. Here is a small code that will give us an Implicit declaration of function error.

Can I skip the declaration of a function in C99?

In the case of C99, you can skip the declaration but it will give us a small warning and it can be ignored but the definition of the function is important. This gives us the output:


2 Answers

One difference is that enclosing it in parenthesis prevents the function-like macros expansion by the preprocessor. As mentioned in the other answers it makes no difference though to the actual compiler.

For instance:

// somewhere buried deep in a header
#define function(a, b) a + b

// your code
void function() {  // this expands the macro and gives compilation error
}

void (function)() { // this does not expand and works as expected
}

This comes in handy for instance when the bright minds behind the Microsoft Visual Studio library decided to provide function-like macros for things like min and max. (There are other ways like #undef to go around this).

Note that object-like macros (e.g. #define function 3 + 4) are still expanded.


The preprocessor is just a dumb text replacement tool (as opposed to the compiler which is just a (smart) text replacement tool). It takes the macro definition and replaces it everywhere. He is not aware of the semantics of what he replaces.

For instance:

// somewhere buried deep in a header
#define function 3 + 2

// your code
void function() {
}

The preprocessor sees the word function and textually replaces it with the string 3 + 2. He is unaware that function is a id-name part of a function declaration and definition. After the preprocess phase there come the actual compile phases. So the compiler actually sees:

// your code
void 3 + 2() {
}

which does not make any sense to him and gives an error.

For function-like macros

// somewhere buried deep in a header
#define function(a, b) a + b

The preprocessor does the same except that it expects two ‘tokens’ enclosed in parenthesis separated by comma (the parameters) and does the replacement. (again no semantics aware):

int d = function(2, 3);
//will be replaced by the preprocessor to:
int d = 2 + 3; // passes compilation phase

void function();
// the preprocessor doesn’t find the arguments for function so it gives an error.

However if it encounters (function) it will not try to expand it (it ignores it). It is just a rule.

like image 117
bolov Avatar answered Sep 27 '22 20:09

bolov


it's the same as

void function();

you can declare it as

void ((function)) ();

if you want :)

be careful not to mix this up with the function pointer declaration syntax.

like image 37
Shikamu Avatar answered Sep 27 '22 20:09

Shikamu