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

I saw two styles of defining overload of a conversion operator in C ++,

  • operator int* (void) const
  • operator int*() const

Question 1. I think that two styles (whether add voidor not) have the same function, right?
Question 2. Any preferences that are better?

+3
source share
5 answers

This applies not only to conversion operators, but also to all functions in C ++ that take no parameters. Personally, I prefer to skip voidfor consistency.

C. , C , , .

, " " . " ", (void).

++ , () (void) .

+10

FAQ ISO ++, f(void) f()?

C f(void) , , ++ . f(void) "" , ++, , - C , , Unix.

++, f(). f(void) ++, C.

FTW:)

+8

++ foo() foo (void) - " ". C99 "undefined ", " ".

, foo() C, .

, , . foo() , foo (void), , . Python:)

+4

. C int name(...). void , . , ( . , ). ++ (void) -. . , ++ libs.

0

I believe in "older" C (I don’t know which version) foo()means "any parameters", while it foo(void)does not mean any parameters. foo()the version of any parameters is outdated. I believe in c99.

A quick googling search finds this wikipedia article mentioning a 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().

0
source

Source: https://habr.com/ru/post/1710899/


All Articles