34

I've seen methods with the following signature:

void foo (void);

They take no argument, however I'm wondering whether doing this is useful or not. Is there a reason why you would want to do it?

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 2
    duplicate http://stackoverflow.com/questions/51032/is-there-a-difference-between-foovoid-and-foo-in-c-or-c – e-MEE Sep 14 '11 at 06:50
  • @nabulke I read that explanation months ago while I was going through parashift, guess I forgot about it :) – Luchian Grigore Sep 14 '11 at 06:57
  • 3
    One may wonder why autogenerated constructor/destructors even in _Visual Studio 2010_ will still result in this code: `CClassname(void)` and `~CClassname(void)` – nabulke Sep 14 '11 at 07:25
  • 1
    @nabulke: If it ain't broke, don't fix it. – Jon Sep 14 '11 at 09:53
  • @nabulke Your link doesn't work now, I believe [this](https://isocpp.org/wiki/faq/newbie#void-in-param-list) is it's new location? – Mark A. Ropper Jun 23 '15 at 18:28
  • [nice explanation here](https://isocpp.org/wiki/faq/newbie#void-in-param-list) – nabulke Jun 24 '15 at 06:04

4 Answers4

54

This is a holdover from older versions of C, where foo() meant "a function with an unknown number of parameters" and foo(void) means "a function with zero parameters." In C++, foo() and foo(void) both mean "a function with zero parameters", but some people prefer the second form because it is more explicit.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • 7
    It's not just older versions of C. In all versions of C starting with the 1989 ANSI standard, `void foo(void);` means that foo has no parametera, and `voi foo();` means that foo has a fixed but unspecified number and type(s) of parameters. C++ accepts the same syntax for compatibility with C. – Keith Thompson Sep 14 '11 at 06:52
  • 20
    More precisely: in "older" (read "very old") versions of C, `foo()` was the only way to declare a function, and there was no verification of the number or types of arguments. When C adopted C++'s function prototypes, there was a backwards compatibility issue with `f()` (since in C++, it meant no parameters, where as it didn't say anything about the parameters in C). The C standards committee invented `f(void)` as a work-around for this, and the C++ committee adopted it for reasons of C compatibility. – James Kanze Sep 14 '11 at 07:43
  • @James: I think you meant that the *C* standards committee invented it. – Keith Thompson Sep 14 '11 at 07:46
  • @Keith Definitely. Thanks for pointing it out; I'll edit my comment to correct this. (Except that I can't. This seems to be the only comment I can edit.) – James Kanze Sep 14 '11 at 08:42
  • So if you have `void foo()` and this can have parameters, does that mean you can call it like foo(1)? If so, how do you access those parameters in the function if you didn't name the parameters? – gsgx Jun 02 '13 at 21:06
  • 1
    @gsingh2011 You have to declare the parameters in the definition. Old versions of C did not allow declaring parameters in the forward declaration. For more details, read the C standard. – Raymond Chen Jun 02 '13 at 22:57
17

The C++03 standard says (emphasis mine):

8.3.5.2

The parameter-declaration-clause determines the arguments that can be specified, and their processing, when the function is called. [Note: the parameter-declaration-clause is used to convert the arguments specified on the function call; see 5.2.2. ] If the parameter-declaration-clause is empty, the function takes no arguments.

This means that if you are talking to the compiler it's just a matter of taste.

If you are writing code that will be read by others, then the C++ way of doing things is

void foo();

The other form remains valid only for reasons of compatibility with C, where there was a difference among the two signatures.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • @DavidHeffernan: Doesn't "it's a matter of taste" implicitly answer that? Note, I 'm only taking "fresh" C++ code into account and not code which used to be C in the past but was ported, in which case "because nobody bothered to change it to the modern way" might be an answer. – Jon Sep 14 '11 at 09:43
  • 1
    @DavidHeffernan: I can see the value in that, so I made an edit to that effect. Thank you for your input. – Jon Sep 14 '11 at 09:52
3

This is a legacy from the older versions of C for functions with no arguments.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gilad Naor
  • 20,752
  • 14
  • 46
  • 53
3

In C++ code there is no reason whatsoever to use void in this way. What's more it is very much not the idiomatic way to declare parameterless functions.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490