Why is a function without argument identifiers valid in C++?

前端 未结 5 2030
粉色の甜心
粉色の甜心 2021-02-06 22:05

Given a function in C++ with arguments that are only types and have no identifiers,

 void foo1(int, int, int){cout << \"called foo1\";}

I

5条回答
  •  [愿得一人]
    2021-02-06 22:11

    The idea is that you might want to change the function definition to use the placeholder later, without changing all the code where the function is called.

    Arguments in a function declaration can be declared without identifiers. When these are used with default arguments, it can look a bit funny. You can end up with :

    void f(int x, int = 0, float = 1.1);
    

    In C++ you don’t need identifiers in the function definition, either:

    void f(int x, int, float flt) { /* ... */ }
    

    In the function body, x and flt can be referenced, but not the middle argument, because it has no name. Function calls must still provide a value for the placeholder, though: f(1) or f(1,2,3.0). This syntax allows you to put the argument in as a placeholder without using it.

提交回复
热议问题