I'm putting together a C++-based assignment for a class I'm teaching. I have a function I'm exporting to students that I'd like them to call at various points in their program so that, during grading, we can intercept those calls to make sure that they're doing the right things at the right times. I don't want that code to do anything in the provided starter files, so I just gave the function a body that just has a series of statements that casts all the arguments to void
to suppress compiler warnings about unused arguments. In the course of doing so, I ran into an unusual compiler error I've never seen before, and a search over this site didn't turn up anything helpful.
The error can be best exemplified by this reduced test case:
void iDontUseMyArguments(int a, int b) {
(void) a; // Explicit cast to void - totally fine!
(void) b;
}
void iDontEither(int a, int b) {
(void) a, b; // Comma expression casted to void, though technically
// b isn't casted to void!
}
void norDoI(int a, int b) {
void(a, b); // ERROR! No idea why this isn't okay.
}
void meNeither(int a, int b) {
(void)(a, b); // Comma expression casted to void - totally fine!
}
void jumpOnBandwagon(int a, int b) {
void((a, b)); // Comma expression casted to void - totally fine!
}
As you can see, most of these compile just fine. The issue was in this one:
void(a, b);
This triggers the following error:
prog.cpp: In function 'void norDoI(int, int)':
prog.cpp:11:11: error: expression list treated as compound expression in functional cast [-fpermissive]
void(a, b);
^
I've never encountered this error message, so I'm not sure what this is trying to tell me.
The intent behind the line
void(a, b);
was to be a comma expression involving a
and b
that was then casted to type void
using a function-style cast. As you can see, the following variants all work:
(void)(a, b);
void((a, b));
I suspect this likely has something to do with the Most Vexing Parse and this getting interpreted as a declaration, but the particular error I'm getting doesn't seem to match this.
My question is the following:
- Why, exactly, isn't this code legal?
- What does the compiler think I'm trying to do?
- Why, exactly, are these other variants legal?