Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete unused argument names in function definition(coding standards).

Tags:

c++

Herb Suttter C++ coding standards says, It is good practice to delete unused argument names in functions to write zero warning program.

Example:

int increment(int number, int power=0){
   return number++;
}

should be

int increment(int number, int /*power*/=0){
   return number++;
}

If there is 'unused variable warning' to power argument. This works fine for programs (no compile errors), So new function definitions will be

int increment(int number, int =0)

So what does int=0 mean to compiler?

like image 666
Nayana Adassuriya Avatar asked Apr 01 '13 07:04

Nayana Adassuriya


2 Answers

Unnamed formal parameter with a default value equal to 0.

First case (most popular) is an usage in function-declaration, something like

int increment(int, int = 0);

and in definition parameter will be named.

int increment(int number, int power)
{
   //
}

Second case is an usage for debug purposes, or for some features, that are not implemented yet, or for dummy functions.

like image 190
ForEveR Avatar answered Sep 30 '22 16:09

ForEveR


If this is a standalone function, of course, you can change method's signature commenting out last parameter

int increment(int number/*, int power=0*/);

but, you may want to keep method's signature unchanged in case:

  • this is a method which is going to override a method declared in base class
  • it is a part of module's public interface which you don't want to change

Also, default value for unnamed parameter can be useful when you use it in function declaration and later somewhere in cpp file you still give name to that variable.

// Forward declaration
int increment(int number, int =0);

// Somewhere in cpp file:
int increment(int number, int power)
  {
  return pow(number, power);
  }
like image 24
MaksymB Avatar answered Sep 30 '22 16:09

MaksymB