4

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?

Tas
  • 7,023
  • 3
  • 36
  • 51
George2
  • 44,761
  • 110
  • 317
  • 455

5 Answers5

10

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • "when C did not have prototypes" -- you mean in old days, C does not allow declare a function prototype? Curious. Could you show me how old C looks like in this context please? :-) – George2 Jun 20 '09 at 10:27
  • "empty braces were retained for function declarations to mean 'unspecified parameters' for flexibility and backwards compatibility." -- confused about this, I think empty braces should mean no parameter, not 'unspecified parameters'? Any comments? – George2 Jun 20 '09 at 10:28
  • 1
    What do you mean 'should mean'? It's what they do mean in C++, but not what they mean in C. If C had always had prototypes then, yes, perhaps () could have been used to mean 'no parameters' as well. – CB Bailey Jun 20 '09 at 10:35
  • 1
    There some old vs. new style examples in this HP manual: http://g4u0420c.houston.hp.com/en/B3901-90016/ch03s15.html – CB Bailey Jun 20 '09 at 10:41
  • Thanks Charles, 1. let me confirm, I think you mean in C++ they are always the same correct? 2. Why in C they may be different? Could you recommend me some documents to read or show some samples to illustrate please? – George2 Jun 20 '09 at 16:44
9

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 :)

legends2k
  • 31,634
  • 25
  • 118
  • 222
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
4

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 :)

oggy
  • 1,855
  • 1
  • 13
  • 10
  • Thanks, I prefer saving type too! – George2 Jun 20 '09 at 10:26
  • A further question, in C++ they are the same, but in C, what is the answer? – George2 Jun 20 '09 at 16:43
  • In C, a declaration of foo() means "undefined arguments", both in number and in type. – oggy Jun 21 '09 at 16:02
  • I can imagine a useful use of undefined arguments in C in a function pointer that can take, say, only one argument, but of an unknown type: `void (*func)() = /* func that takes an int */; func(11); func = /* func that takes a char * */; func("hello");` But this is of dubious value. – Chris Lutz Jan 06 '10 at 02:44
0

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().

stefanB
  • 77,323
  • 27
  • 116
  • 141
-1

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.