Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How to warning about difference between argument names in function declaration and definition

Is there a way to get warned about missmatching of the argument names between a function declaration and its definition?

Declaration

double divide(int a, int b);

Definition

double divide(int b, int a)
{
    return a / b;
}

For a user, that use the function divide, will expect a / b as a result and not b / a.

I know, the compiler can't do that, but are there some static analysis tools that can do that? If yes, which ones?

like image 901
lucaboni Avatar asked Nov 04 '16 10:11

lucaboni


People also ask

What's the difference between function declaration and function definition?

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

What happens if one of the argument names in a function declaration does not match that of the corresponding function definition?

It is an error if the number of arguments in a function definition, declaration, or call does not match the prototype. If the data type of an argument in a function call does not match the corresponding type in the function prototype, the compiler tries to perform conversions.

What is the difference between definition and declaration of a variable and a function?

Declaration means that variable is only declared and memory is allocated, but no value is set. However, definition means the variables has been initialized. The same works for variables, arrays, collections, etc.

Do parameter names and argument names have to be the same?

The caller's arguments passed to the function's parameters do not have to have the same names.


1 Answers

You can use clang-tidy. Calling it a compiler is a bit of a stretch, but maybe there is an option to make clang emit clang-tidy warnings. The specific option you want is readability-inconsistent-declaration-parameter-name.

like image 81
nwp Avatar answered Oct 07 '22 00:10

nwp