Commented out formal parameter?

R

Roy Smith

I've got some legacy code I'm maintaining. There's a method declared in
the class.h file like this:

void foo(unsigned priority);

and it's implemented as:

void myClass::foo (unsigned /* priority */)
{
// body omitted
}

The parameter priority isn't actually used anywhere in the body (I can only
assume it was at one point in the history of the code).

What I don't understand is how this compiles (and compile it does, under 5
different compilers). What does the compiler think the signature of the
method is?
 
C

Cy Edmunds

Roy Smith said:
I've got some legacy code I'm maintaining. There's a method declared in
the class.h file like this:

void foo(unsigned priority);

and it's implemented as:

void myClass::foo (unsigned /* priority */)
{
// body omitted
}

The parameter priority isn't actually used anywhere in the body (I can
only
assume it was at one point in the history of the code).

What I don't understand is how this compiles (and compile it does, under 5
different compilers). What does the compiler think the signature of the
method is?

The name of the formal parameter isn't part of the signature.
 
R

Roy Smith

"Cy Edmunds said:
The name of the formal parameter isn't part of the signature.

OK, that's fair enough, but maybe I phrased my question a little to
explicitly. What, exactly, does the compiler think the definition means.
Is it a function which takes one argument of type unsigned, with no name?
If so, what does it mean to have an argument with no name?
 
C

Cy Edmunds

Roy Smith said:
OK, that's fair enough, but maybe I phrased my question a little to
explicitly. What, exactly, does the compiler think the definition means.
Is it a function which takes one argument of type unsigned, with no name?
If so, what does it mean to have an argument with no name?

It is a function which takes an unsigned int as an argument and returns
void. The name has nothing to do with it. When calling the function you must
provide an unsigned int, either as a constant or as a variable of that type
or a type convertable to that type.

The fact that the argument has no name in the implementation means that the
implementation can't make any use of the argument value.
 
A

Andre Kostur

OK, that's fair enough, but maybe I phrased my question a little to
explicitly. What, exactly, does the compiler think the definition
means. Is it a function which takes one argument of type unsigned,
with no name? If so, what does it mean to have an argument with no
name?

The compiler thinks it means that the method returns nothing, and takes
an unsigned by value. However since it has no name, it cannot be
referenced (in any sane manner). Usually it's done (the commenting out
of the name) so that the function retains the signature with the unsigned
parameter (perhaps part of a virutal method hierarchy?) but this
implementation doesn't use the parameter. And you don't want your
compiler warning you about an unused parameter.
 
K

Kaz Kylheku

Roy said:
I've got some legacy code I'm maintaining. There's a method declared in
the class.h file like this:

void foo(unsigned priority);

and it's implemented as:

void myClass::foo (unsigned /* priority */)
{
// body omitted
}

The parameter priority isn't actually used anywhere in the body (I can only
assume it was at one point in the history of the code).

What I don't understand is how this compiles (and compile it does, under 5
different compilers). What does the compiler think the signature of the
method is?

The name of a parameter isn't part of the function's type signature.
That has never been the case, not in the C language, nor in C++.

In C you could write a prototype declaration without naming the
parameters, as in:

extern void foo(unsigned); /* valid ANSI C going back to the 1980's
*/

In C++, it is permissible to omit a parameter name in a function
definition also. This is the means by which the programmer can express
that a parameter is deliberately ignored.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top