Why add void to method parameter list

后端 未结 4 1924
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 06:53

I\'ve seen methods with the following signature:

void foo (void);

They take no argument, however I\'m wondering whether doing this is usefu

相关标签:
4条回答
  • 2020-12-03 07:06

    This is a legacy from the older versions of C for functions with no arguments

    0 讨论(0)
  • 2020-12-03 07:09

    In C++ code there is no reason whatsoever to use void in this way. What's more it is very much not the idiomatic way to declare parameterless functions.

    0 讨论(0)
  • 2020-12-03 07:14

    The C++03 standard says (emphasis mine):

    8.3.5.2

    The parameter-declaration-clause determines the arguments that can be specified, and their processing, when the function is called. [Note: the parameter-declaration-clause is used to convert the arguments specified on the function call; see 5.2.2. ] If the parameter-declaration-clause is empty, the function takes no arguments.

    This means that if you are talking to the compiler it's just a matter of taste.

    If you are writing code that will be read by others, then the C++ way of doing things is

    void foo();
    

    The other form remains valid only for reasons of compatibility with C, where there was a difference among the two signatures.

    0 讨论(0)
  • 2020-12-03 07:17

    This is a holdover from older versions of C, where foo() meant "a function with an unknown number of parameters" and foo(void) means "a function with zero parameters." In C++, foo() and foo(void) both mean "a function with zero parameters", but some people prefer the second form because it is more explicit.

    0 讨论(0)
提交回复
热议问题