views:

463

answers:

6

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?

+2  A: 

This is really a duplicate of a previously asked question:

http://stackoverflow.com/questions/693788/c-void-arguments

Does that help?

Daniel Earwicker
Not really a duplicate, since that other question was explicitly about C, and this question is explicitly about C++, and the answer is different between the two. In C++ it's purely a cosmetic decision.
Steve Jessop
Yeah, really it's my answer to that question that is the duplicate of the correct answer to this question.
Daniel Earwicker
@Earwicker, I read the discussion but confused about two points. 1. why In C void f() means "could take any number of parameters of unknown types"? 2. Maybe I am not developing in old days when prototype is not invented, I am confused but interested to learn the method in C of using "Identifier lists" method to declare a function. Could you recommend me some documents to read?
George2
Daniel Earwicker
This document is extremely excellent, but it does not answer my question, why void f() could match any number of input parameters? Which is mentioned here, http://stackoverflow.com/questions/693788/c-void-arguments In C it means "could take any number of parameters of unknown types". Any ideas?
George2
What do you mean "Why?" I guess the designers of the C programming language thought that would be a good idea. The predecessor languages of C (such as BCPL) didn't do any type checking at all (in fact BCPL didn't even have types). The idea that such things should be checked at all was regarded as a "luxury" by OS programmers in 1970. Today, there's no good reason to put up with such historical accidents unless you are maintaining ancient code.
Daniel Earwicker
+9  A: 

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.

Charles Bailey
"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
"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
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.
Charles Bailey
There some old vs. new style examples in this HP manual: http://g4u0420c.houston.hp.com/en/B3901-90016/ch03s15.html
Charles Bailey
+1. also, nice article.
Johannes Schaub - litb
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
+4  A: 

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
Thanks, I prefer saving type too!
George2
A further question, in C++ they are the same, but in C, what is the answer?
George2
In C, a declaration of foo() means "undefined arguments", both in number and in type.
oggy
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
+2  A: 

http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.4

Quote from answer:

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

Merlyn Morgan-Graham
To bad i started my answer before you posted :X. +1
acidzombie24
A: 

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.

acidzombie24
A: 

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