18

In C++ using void in a function with no parameter, for example:

class WinMessage
{
public:
    BOOL Translate(void);
};

is redundant, you might as well just write Translate();.

I, myself generally include it since it's a bit helpful when code-completion supporting IDEs display a void, since it ensures me that the function takes definitely no parameter.

My question is, Is adding void to parameter-less functions a good practice? Should it be encouraged in modern code?

ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
  • 4
    @awoodland: "I got a hangover from C." Mhmm. Seems fitting. `:)` – sbi Mar 03 '12 at 10:10
  • Here is the Link that might help http://stackoverflow.com/questions/7412274/why-add-void-to-method-parameter-list –  Mar 03 '12 at 10:10

5 Answers5

27

In C++

void f(void);

is identical to:

void f();

The fact that the first style can still be legally written can be attributed to C.
n3290 § C.1.7 (C++ and ISO C compatibility) states:

Change: In C++, a function declared with an empty parameter list takes no arguments.

In C, an empty parameter list means that the number and type of the function arguments are unknown.

Example:

int f(); // means int f(void) in C++
         // int f( unknown ) in C

In C, it makes sense to avoid that undesirable "unknown" meaning. In C++, it's superfluous.

Short answer: in C++ it's a hangover from too much C programming. That puts it in the "don't do it unless you really have to" bracket for C++ in my view.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • Is there any equivalent to C's `int f()` in C++? Should I use `external "C"` for this use case? – Spidey Jul 01 '14 at 15:01
  • @Spidey why would you want to do that? It's undefined behaviour if you get it wrong and a legacy hangover from early C days to reduce compiler/linker complexity. In C++11 variadic templates are a safe, modern feature though but not a direct equivalent. – Flexo Jul 01 '14 at 15:21
  • I've used it as a type for a function pointer array. We exchanged array pointers between client and dll code, and thus avoid the overhead of calling dll functions constantly. – Spidey Jul 02 '14 at 22:04
  • @Spidey If you want platform specific then I'd just make it an array of `FARPROC` and cast for calls. – Flexo Jul 02 '14 at 22:13
  • I can't find after a fast and basic search if FARPROC is defined by the standard. I'm using C89 in embedded devices, I don't think I can depend on FARPROC being defined. By the way, what is FARPROC definition? I bet it is `int f()` or `void *f()`. – Spidey Jul 02 '14 at 22:19
  • In windef.h: typedef int (FAR WINAPI *FARPROC)(); http://forums.devarticles.com/showpost.php?p=99993&postcount=2 – Spidey Jul 02 '14 at 22:20
  • FARPROC is windows specific, but if you're relying on undefined behaviour as an optimisation for windows specific case then that's an upgrade. (Happy to chat more in: http://chat.stackoverflow.com/rooms/10/loungec before this goes any further off-topic) – Flexo Jul 02 '14 at 23:27
6

I see absolutely no reason for this. IDEs will just complete the function call with an empty argument list, and 4 characters less.

Personally I believe this is making the already verbose C++ even more verbose. There's no version of the language I'm aware of that requires the use of void here.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • 12
    If you think C++ is verbose, try Java. `:)` – sbi Mar 03 '12 at 10:11
  • Depends on what Java version you are talking about and also on what kind of work you do. Post Java 8, the language is not as verbose, and C++ metaprogramming can be quite tangled (to put it mindly.) PS. I work with both languages professionally. They both can be verbose depending what you are doing with either. – luis.espinal Feb 25 '22 at 14:07
5

I think it will only help in backward compatibility with older C code, otherwise it is redundant.

Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44
3

I feel like no. Reasons:

  • A lot more code out there has the BOOL Translate() form, so others reading your code will be more comfortable and productive with it.
  • Having less on the screen (especially something redundant like this) means less thinking for somebody reading your code.
  • Sometimes people, who didn't program in C in 1988, ask "What does foo(void) mean?"
Bingo
  • 3,785
  • 6
  • 23
  • 27
0

Just as a side note. Another reason for not including the void is that software, like starUML, that can read code and generate class diagrams, read the void as a parameter. Even though this may be a flaw in the UML generating software, it is still annoying to have to go back and remove the "void"s if you want to have clean diagrams