Using void in functions without parameter?

前端 未结 5 974
说谎
说谎 2021-02-19 07:44

In C++ using void in a function with no parameter, for example:

class WinMessage
{
public:
    BOOL Translate(void);
};

is redunda

5条回答
  •  再見小時候
    2021-02-19 08:39

    In C++

    void f(void);
    

    is identical to:

    void f();
    

    The fact that the first style can still be legally written can be attributed to C.
    n3290 § C.1.7 (C++ and ISO C compatibility) states:

    Change: In C++, a function declared with an empty parameter list takes no arguments.

    In C, an empty parameter list means that the number and type of the function arguments are unknown.

    Example:

    int f(); // means int f(void) in C++
             // int f( unknown ) in C
    

    In C, it makes sense to avoid that undesirable "unknown" meaning. In C++, it's superfluous.

    Short answer: in C++ it's a hangover from too much C programming. That puts it in the "don't do it unless you really have to" bracket for C++ in my view.

提交回复
热议问题