C ++ void, prefix to function call. eg. `main () {void func ();}`

void func() {assert(0);} int main () {void func();} 

The above code does not call func () or at least does not reach the statement. Not that I really need to know, but I'm just wondering what's going on here?

+4
source share
4 answers

You declare a prototype for a function named func that returns nothing and takes no arguments. This (one of) is a subtle difference between function calls and function prototypes. Note that the line above main , void func() {assert(0);} does not affect whether it is a prototype or a call. You can delete it, and the code will do the same - that is, nothing.

It also tells you that you can reuse function prototypes. You can even have this:

 int main() { void blah(); void blah(); void blah(); void blah(); } 

And the code will still do what it did before - nothing.

If you leave void , this will call the function.

Also note that in the case of a function that accepts parameters, this is:

 int main() { func(4); } 

will not turn into a prototype if you added void before it looks like this:

 int main() { void func(4); } 

it just throws a syntax error.

+10
source

As others pointed out, the line

 void func(); 

inside main considered as a function prototype, not a func function call. In C and C ++, you can declare function prototypes inside functions if you want, although this is rarely done in practice.

The fact that this is legal causes all kinds of headaches for programmers. For example, if you rewrote the code as

 (void) func(); 

This will then compile as a call to func , whose return type is explicitly indicated by void to indicate "I don't care about the return value." In other words, this set of parentheses changes the declaration into an operator.

In C ++, this problem can be compounded by the fact that this code below is a function prototype, and not a variable declaration that calls the default constructor:

 Object myObject(); 

Although

 Object myObject(137); 

creates an object and passes 137 to its constructor, and

 Object myObject; 

creates an object without calling the constructor.

There is a terrible regional case of the language called the โ€œmost unpleasant analysisโ€ that occurs when you try to declare an object when you call its constructor. For example, this code is legal C ++, but it is a function declaration, not a variable declaration:

 set<int> mySet(istream_iterator<int>(cin), istream_iterator<int>()); 

The problem is that this can be parsed as a function declaration, rather than creating an object that takes two istream_iterator<int> time parameters. To fix this, in C ++ you need to write

 set<int> mySet(istream_iterator<int>(cin), (istream_iterator<int>())); 

Where, as indicated above, additional parentheses forcefully eliminate the inconsistency of the operator as a prototype of the function as the declaration.

Hope this helps!

+5
source

You can declare functions even if it is not needed. This is what you did, re-declared the function.

+4
source

You declare a local function void func() inside main() . The void statement tells the compiler that it is a declaration, not a function call. So delete void , your function will be called.

0
source

All Articles