Why are function argument names unimportant in c++ declarations?

时间:2020-12-10 13:57:25

Function argument names in declarations (that most likely reside in the header file) are seemingly completely ignored by the compiler. What are the reasons for allowing the following to compile using either declaration version 1 or 2?

声明中的函数参数名称(很可能存在于头文件中)似乎被编译器完全忽略。允许以下使用声明版本1或2进行编译的原因是什么?


implementation

履行

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

declaration - Version 1

声明 - 第1版

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

declaration - Version 2

声明 - 第2版

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

5 个解决方案

#1


11  

The compiler only needs to know what kind of arguments the method requires. It's unimportant for the compiler how you call them.

编译器只需要知道方法需要什么类型的参数。编译器如何调用它们并不重要。

The compiler needs to know the argument types for several reasons:

编译器需要知道参数类型有以下几个原因:

  • Decide which method to use if there are several methods with the same method name
  • 如果有多个方法具有相同的方法名称,请确定使用哪种方法
  • Decide whether the input parameters are valid
  • 确定输入参数是否有效
  • Decide whether the parameters need to be casted
  • 确定是否需要铸造参数
  • Decide how to generate the CODE to call the method and handle the response
  • 决定如何生成CODE来调用方法并处理响应

However, I suggest to use the first header version. It helps other developers (and yourself) to use the functions and know what parameters have which meaning.

但是,我建议使用第一个标题版本。它可以帮助其他开发人员(和您自己)使用这些函数并知道哪些参数具有哪些含义。

#2


6  

Parameter names aren't part of the function signature. Unless you use them, you don't need to have names even in the function implementation.

参数名称不是函数签名的一部分。除非您使用它们,否则即使在函数实现中也不需要具有名称。

#3


5  

Because the names don't affect anything the compiler does outside the function.

因为名称不会影响编译器在函数外部执行的任何操作。

#4


1  

The only reason I can think about that version 1 is better is readability. They are ignored as they don't matter for the compiler.

我能想到版本1的唯一原因是可读性更好。它们被忽略,因为它们对编译器无关紧要。

#5


1  

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

..因为当标题包含在其他模块中时,它只需要类型来生成正确的代码。名称ae通常有用且方便,但绝对必要。

#1


11  

The compiler only needs to know what kind of arguments the method requires. It's unimportant for the compiler how you call them.

编译器只需要知道方法需要什么类型的参数。编译器如何调用它们并不重要。

The compiler needs to know the argument types for several reasons:

编译器需要知道参数类型有以下几个原因:

  • Decide which method to use if there are several methods with the same method name
  • 如果有多个方法具有相同的方法名称,请确定使用哪种方法
  • Decide whether the input parameters are valid
  • 确定输入参数是否有效
  • Decide whether the parameters need to be casted
  • 确定是否需要铸造参数
  • Decide how to generate the CODE to call the method and handle the response
  • 决定如何生成CODE来调用方法并处理响应

However, I suggest to use the first header version. It helps other developers (and yourself) to use the functions and know what parameters have which meaning.

但是,我建议使用第一个标题版本。它可以帮助其他开发人员(和您自己)使用这些函数并知道哪些参数具有哪些含义。

#2


6  

Parameter names aren't part of the function signature. Unless you use them, you don't need to have names even in the function implementation.

参数名称不是函数签名的一部分。除非您使用它们,否则即使在函数实现中也不需要具有名称。

#3


5  

Because the names don't affect anything the compiler does outside the function.

因为名称不会影响编译器在函数外部执行的任何操作。

#4


1  

The only reason I can think about that version 1 is better is readability. They are ignored as they don't matter for the compiler.

我能想到版本1的唯一原因是可读性更好。它们被忽略,因为它们对编译器无关紧要。

#5


1  

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

..因为当标题包含在其他模块中时,它只需要类型来生成正确的代码。名称ae通常有用且方便,但绝对必要。