tolower 发生了什么事?

What's going on at tolower?

我正在阅读这个答案:

这部分是怎么回事:(int (*)(int))tolower 在这行代码中:

transform(s.begin(),s.end(),s.begin(),(int (*)(int))tolower );

如果我创建这个函数并且代码有效:

int myToLower(int c){
    return tolower(c);
}

transform(s.begin(),s.end(),s.begin(), myToLower);

这部分的简单英语是什么:(int (*)(int))

您可以在 my answer, that's a function pointer. You can read more about them here: http://en.cppreference.com/w/cpp/language/pointer#Pointers_to_functions

中看到这个答案

从根本上说,这是一个指向函数的指针,该函数接受一个 int 参数和 returns 一个 int.


transform 在使用 myToLower 而不是未转换的 tolower 时起作用的原因是在代码中 tolower 函数在 std 命名空间由语言环境库的 tolower and the ctype library's tolower 命名空间。当仅将函数名称用作未转换指针时,不会执行重载决策,并且您会收到错误。当您强制转换函数指针时,您是在告诉编译器您想要哪个重载。