Can an unnamed parameter of function have a default value?

后端 未结 4 1363
北荒
北荒 2021-02-20 00:36

Is the following code legal in C++?

void f(void* = 0)
{}

int main()
{
    f();
}

Which page of the C++ standard states that this usage is lega

4条回答
  •  感动是毒
    2021-02-20 01:33

    Not only is it legal, it could actually be quite useful depending on your coding style.

    Default parameters are only meaningful in a function declaration.

    Named parameters are only meaningful in a function definition.

    f.h:

    void f(void*=nullptr);
    

    f.cc

    void f(void* x)
    {
    ...
    }
    

提交回复
热议问题