In C++, you can utilize lambda functions to achieve something similar:
#include <iostream>
using CmpFunc = bool(float, float);
CmpFunc* condition(char op) {
switch (op) {
case '>':
return [](float a, float b) { return a > b; };
case '<':
return [](float a, float b) { return a < b; };
case '=':
return [](float a, float b) { return a == b; };
default:
return [](float a, float b) { return false; };
}
}
int main() {
auto const cmp = condition('>');
if (cmp(7, 6)) {
std::cout << "a > b";
}
}
solved How to convert string expression to boolean in c++? [closed]