Search code examples
c++functiondefaultcalling-convention

Why should default parameters be added last in C++ functions?


Why should default parameters be added last in C++ functions?


Solution

  • To simplify the language definition and keep code readable.

    void foo(int x = 2, int y);
    

    To call that and take advantage of the default value, you'd need syntax like this:

    foo(, 3);
    

    Which was probably felt to be too weird. Another alternative is specifying names in the argument list:

    foo(y : 3);
    

    A new symbol would have to be used because this already means something:

    foo(y = 3); // assign 3 to y and then pass y to foo.
    

    The naming approach was considered and rejected by the ISO committee because they were uncomfortable with introducing a new significance to parameter names outside of the function definition.

    If you're interested in more C++ design rationales, read The Design and Evolution of C++ by Stroustrup.