George2
George2

Reputation: 45801

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

I have seen two styles of defining conversion operator overload in C++,

  1. operator int* (void) const
  2. 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?

Upvotes: 4

Views: 1720

Answers (5)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59131

Quoting from ISO C++ FAQ, Should I use f(void) or f()?

C programmers often use f(void) when declaring a function that takes no parameters, however in C++ that is considered bad style. In fact, the f(void) style has been called an "abomination" by Bjarne Stroustrup, the creator of C++, Dennis Ritchie, the co-creator of C, and Doug McIlroy, head of the research department where Unix was born.

If you're writing C++ code, you should use f(). The f(void) style is legal in C++, but only to make it easier to compile C code.

Appeal to authority FTW :)

Upvotes: 9

stefanB
stefanB

Reputation: 79840

I believe in 'older' C (don't know what version) foo() meant 'any parameters' whereas foo(void) meant no parameters. foo() 'any parameters' version has been deprecated I believe in c99.

Quick googling finds this wikipedia article mentioning similar fact.

C++ will accept foo(void) but it means the same as foo() which means 'no parameters'.

So in C++ the preferred way is to use foo().

Upvotes: 0

user34537
user34537

Reputation:

omit the void. In old style C all functions were assumed to be int name(...). Specifying void meant it was not a variable length parameter. That default was removed and all functions had to be specified (thankfully. It was the wild west when anything could be anything). In C++ you dont need to write (void) ever. Omit it. Just as the C++ libs do.

Upvotes: -1

oggy
oggy

Reputation: 1859

In C++ foo() and foo(void) are the same - "no arguments". In the C99 standard, the former means "undefined number of arguments", while the latter means "no arguments".

However, if you rely on the foo() behavior in C, you should be shot.

So this means that you can use either. Now personally, I like foo() better than foo(void), since I hate visual clutter, but that's just preference. I'm a Python guy :)

Upvotes: 4

CB Bailey
CB Bailey

Reputation: 792517

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.

Upvotes: 10

Related Questions