Which is preferred: foo(void) or foo() in C++

c++, void

Solution

This doesn't just apply to conversion operators but to all functions in C++ that take no parameters. Personally, I prefer to omit `void` for consistency.

The practice originates from C. Originally, when C did not have prototypes, an empty pair of braces was used in function declarations and did not provide any information about the parameters that the function expected.

When prototypes were added, empty braces were retained for function declarations to mean 'unspecified parameters' for flexibility and backwards compatibility. To provide an explicit prototype meaning 'takes no parameters', the syntax `(void)` was added.

In C++ all function declarations have to have prototypes, so `()` and `(void)` have the same meaning.

Problem

I have seen two styles of defining conversion operator overload in C++, - `operator int* (void) const` - `operator int*() const` Question 1. I think the two styles (whether add `void` or not) have the same function, correct? Question 2. Any preference which is better?

Original source

Related problems