When I define a function pointer in C++ using the ternary operator I get the compiler error overloaded function with no contextual type information
.
I'm confused. Could someone explain me the reason of this behavior?
#include <string>
#include <string.h>
#include <iostream>
const char *my_strstr1 (const char *__haystack, const char *__needle) {
std::cout << "my_strstr" << std::endl;
return strstr(__haystack, __needle);
}
const char *my_strstr2 (const char *__haystack, const char *__needle) {
std::cout << "my_strstr2" << std::endl;
return strstr(__haystack, __needle);
}
int main(int argc, char** argv) {
std::cout << "argc:" << argc << std::endl;
//ok
// const char* (*StrStr)(const char*, const char*) = strstr;
// const char* (*StrStr)(const char*, const char*) = (argc > 1) ? my_strstr2 : my_strstr1;
// error: overloaded function with no contextual type information
const char* (*StrStr)(const char*, const char*) = (argc > 1) ? my_strstr1 : strstr;
StrStr("helloworld", "h");
return 0;
}