I'm trying to use a function pointer with template as an argument. But the compiler seems to have trouble handling lambda and nullptr.
Everything is fine when I change void (*callback)(T input) to void (*callback)(int input) in the following code.
Is this compiler behavior specified by the C++ standard?
The compile command I use is $ g++ main.cpp -std=c+11 but the same behavior found in Visual Studio 2019.
template <class T>
int dummy (T tmp, void (*callback)(T input)) {
// Doesn't do anything, just trying to compile
// If I change (T input) to (int input), it compiles fine
if (callback)
return 1;
else
return 0;
}
void callback (int input) {
return;
}
int main () {
int tmp = 10;
auto callback_lambda = [](int input) -> void {
return;
};
dummy(tmp, callback); // Compiles OK
dummy(tmp, callback_lambda); // Error: mismatched types 'void (*)(T)' and 'main()::<lambda(<int>)'
dummy(tmp, nullptr); // Error: no matching function for call to 'dummy(int&, std:nullptr_t)'
return 0;
}