为什么大多数C++编译器能够推断出::isspace的类型并隐式地将其转换为std::function,而不能对std::isspace执行此操作
请看不能编译的the following:
#include <cctype>
#include <functional>
template <typename Functish>
bool bar1(Functish f) { return f('a'); }
inline bool bar2(std::function<bool(char)> f) { return f('a'); }
#if 1
#define ff &std::isspace
#else
#define ff &::isspace
#endif
#if 0
bool foo()
{
return bar1(ff);
}
#else
bool foo()
{
return bar2(ff);
}
#endif在Compiler Explorer支持的编译器中,ELLCC似乎是唯一一个std::isspace具有我期望的可演绎/可转换性的编译器。
发布于 2017-09-14 08:54:31
std::isspace有多个重载,但只有一个::isspace。
header声明了一个函数int std::isspace(int),也可能在全局名称空间中声明了相同的函数(虽然不必这样做)。
header定义了函数模板template <class CharT> bool std::isspace(CharT, const std::locale&)。
头声明了一个函数int isspace(int),它也可以在名称空间std中声明相同的函数(它不需要这样做)。
在除了ELLCC之外的编译器上,似乎有可能包含(或者两个std::isspace重载都在其他地方声明并包含在两个头文件中)。该标准只指定了标准头部必须声明的符号;它并不禁止它们声明其他不需要的符号。
由于std::isspace是重载的,因此必须将其转换为int(*)(int),以便编译器知道要选择哪个重载。
https://stackoverflow.com/questions/46208733
复制相似问题