Consider the following code:
#include <iostream>
using f = void(std::string);
void fcorrect(f func, std::string s) {
func(s); // calling function
};
void fmistake(f func, std::string s) {
f(s); // calling type alias instead of function
};
void myprint(std::string s) {
std::cout << s << std::endl;
};
int main() {
std::string s = "message";
fcorrect(myprint, s);
fmistake(myprint, s);
return 0;
}
Function fcorrect receives myprint function and a string as arguments and prints this string as expected.
Function fmistake contains a deliberate mistake - it calls type alias f instead of function func. It prints nothing.
I wonder what's happening under the hood when the latter function call takes place? Namely, why no compile time or runtime error are thrown?
As stated by commenters,
f(s)is a declaration. The crucial wording is here:- [stmt.ambig] p1
It doesn't matter what the type is (class, function, etc.),
T(a)is a declaration ifTnames a type.The declaration
f(s)does nothing here because it is not used, and is equivalent tof sorvoid s(std::string).sis already a function parameterstd::string sin this context, so GCC is wrong about allowing it. GCC also allows:This is a known GCC bug 52953, and has first been reported in 2012.