Why Does C++ Allow Unnamed Function Parameters

Why does C++ allow unnamed function parameters?

Yes, this is legal. This is useful for implementations of virtuals from the base class in implementations that do not intend on using the corresponding parameter: you must declare the parameter to match the signature of the virtual function in the base class, but you are not planning to use it, so you do not specify the name.

The other common case is when you provide a callback to some library, and you must conform to a signature that the library has established (thanks, Aasmund Eldhuset for bringing this up).

There is also a special case for defining your own post-increment and post-decrement operators: they must have a signature with an int parameter, but that parameter is always unused. This convention is bordering on a hack in the language design, though.

How to use unnamed function arguments in C or C++

I hope an example can provide some help:

// Declaration, saying there is a function f accepting a double.
void f(double);

// Declaration, saying there is a function g accepting a double.
void g(double);

// ... possibly other code making use of g() ...

// Implementation using the parameter - this is the "normal" way to use it. In
// the function the parameter is used and thus must be given a name to be able
// to reference it. This is still the same function g(double) that was declared
// above. The name of the variable is not part of the function signature.
void g(double d)
{
// This call is possible, thanks to the declaration above, even though
// the function definition is further down.
f(d);
}

// Function having the f(double) signature, which does not make use of
// its parameter. If the parameter had a name, it would give an
// "unused variable" compiler warning.
void f(double)
{
cout << "Not implemented yet.\n";
}

Unnamed parameters in C

VC has a __COUNTER__ macro which gives uniqueness, it appears that GCC has an equivalent macro with the same name from version 4.3.5 (although I can't at the moment find a live link).

Why does C++ code missing a formal argument name in a function definition compile without warnings?

Because sometimes you have a parameter that's required by an interface but the function doesn't use it. Maybe the parameter is no longer necessary, is only necessary in other functions that must use the same signature (especially so they can be called through pointers) or the functionality hasn't been implemented yet. Having parameters that aren't used can be particularly common in generated or framework code for this reason (and that's probably why the MFC generated code has the name commented out).

As to why there's no warning - I guess it's because whether this is a problem is a subjective thing and other people (particularly compiler implementers) don't see it as a problem. Once you actually go to use the parameter, you'll get the compiler to complain if you forget to uncomment the name so you get the compiler complaining only when you really need it to (the compiler's version of the agile YAGNI: "You Aren’t Gonna Neet It" philosophy).

The opposite does seem to generally occur when you crank up warnings - named parameters that aren't used generate warnings - again that's probably why the generated function has the name commented out.

Syntax of an un-named function pointer in C++

Fully explicit form:

Foo bar(Baz f());

bar is a function that takes a single parameter f, which is a function (taking no arguments) returning Baz.

Without naming the parameter:

Foo bar(Baz ());

The reason bar ends up taking a pointer to a function is that functions cannot be passed by value, so declaring a parameter as a function automatically decays it into a pointer. The above declaration is equivalent to:

Foo bar(Baz (*)());

// or:
Foo bar(Baz (*f)()); // with a named parameter

This is similar to void foo(int [10]) where int [10] also means int * in a parameter list.

Mixed named and unnamed function parameters

You are defining a function inline (of type jwt.Keyfunc), but not binding it to anything. If VerifyKey is of type jwt.Keyfunc, then you can just change line 13 to

token, err := jwt.ParseFromRequest(r, VerifyKey)


Related Topics



Leave a reply



Submit