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?