Why are function argument names irrelevant in C ++ declarations?

The argument names of the functions in the declarations (which are most likely in the header file) are apparently completely ignored by the compiler. What are the reasons for compiling the next version using one of versions 1 or 2?


implementation

void A::doStuff(int numElements, float* data) { //stuff } 

Announcement - Version 1

 class A { public: void doStuff(int numElements, float* data); } 

Announcement - Version 2

 class A { public: void doStuff(int, float*); } 
+6
source share
5 answers

The compiler only needs to know what arguments the method requires. For the compiler, it doesn't matter what you call them.

The compiler must know the types of arguments for several reasons:

  • Determine which method to use if there are multiple methods with the same method name.
  • Determine if input parameters are valid.
  • Determine if parameters should be executed
  • Decide how to generate the CODE to call the method and process the response

However, I suggest using the first version of the header. This helps other developers (and you yourself) use the functions and know which parameters matter.

+11
source

Parameter names are not part of the function signature. If you do not use them, you may not need to have names even in the implementation of a function.

+6
source

Since the names do not affect anything, the compiler does it outside the function.

+5
source

The only reason I can think of this version 1 is for better readability. They are ignored because they do not matter to the compiler.

+1
source

.. because when headers are included in other modules, they only need types to generate the correct code. The names ae are often useful and convenient, but nopt is absolutely necessary.

+1
source

Source: https://habr.com/ru/post/913610/


All Articles