Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++, in the definition of functions, the argument identifiers are optional. In which scenario this feature could be useful?

Tags:

c++

int foo(int){      
     ...  
}

Any ideas?

like image 783
mp. Avatar asked Jul 27 '10 21:07

mp.


People also ask

What is the difference between argument and parameter of a function?

Note the difference between parameters and arguments: Function parameters are the names listed in the function's definition. Function arguments are the real values passed to the function. Parameters are initialized to the values of the arguments supplied.

Why do we use parameters in programming?

Parameters allow a function to perform tasks without knowing the specific input values ahead of time. Parameters are indispensable components of functions, which programmers use to divide their code into logical blocks.

What is an argument programming?

An argument is a way for you to provide more information to a function. The function can then use that information as it runs, like a variable. Said differently, when you create a function, you can pass in data in the form of an argument, also called a parameter.


2 Answers

When you are not actually using the parameter in the function but do not want to break the public method signature.

like image 119
Ed S. Avatar answered Oct 11 '22 21:10

Ed S.


One case where you define a function but do not name the parameter is given in Scott Meyers's "Effective C++, 3rd edition", Item 47:

template<typename IterT, typename DistT>        
void doAdvance(IterT& iter, DistT d,            
               std::random_access_iterator_tag) 
{
  iter += d;
}

is used in:

template<typename IterT, typename DistT>
void advance(IterT& iter, DistT d)
{
  doAdvance( iter, d, 
             typename std::iterator_traits<IterT>::iterator_category() );
}

Essentially, the third parameter in doAdvance is a type unaccompanied by a variable name, i.e. it is an unnamed parameter. Having an unnamed parameter isn't a problem, since that argument is only used during the resolution of which overloaded function to use. I discuss these topics in this related SO question.

like image 23
Alexandros Gezerlis Avatar answered Oct 11 '22 22:10

Alexandros Gezerlis