我对C++ 11的功能比较陌生.我对自动功能以及它如何类型推断出仿函数有疑问.请考虑以下代码段:
bool test1(double a, double b) {
return (a<b);
}
bool test2(double a, double b) {
return (a>b);
}
struct Test1 {
bool operator()(double a, double b) {
return (a<b);
}
};
struct Test2 {
bool operator()(double a, double b){
return (a>b);
}
};
int main() {
const bool ascending = false;
auto comparator = ascending? test1:test2; // works fine
auto comparator2 = ascending? Test1():Test2(); // compiler error: imcompatible types
std::function<bool(double, double)> comparator3 = ascending? Test1():Test2(); // compiler error: …Run Code Online (Sandbox Code Playgroud)