I'm using lambdas instead of a comparator class for my set, but I'm running into issues.
I have something like this:
class Processor {
private:
const function<bool(std::string, std::string)> _lenComp = [](string a, string b) { return a != b && a.size() >= b.size(); };
set<string, decltype(_lenComp)> _inputSet;
// ...
public:
Processor() : _inputSet() {
}
void addLine(string line) {
_inputSet.insert(line);
}
When I create a Processor instance and call addLine 2 times I get a bad function call exception.
How do I properly initialize this class?
decltype(_lenComp) is function<bool(std::string, std::string)> which is an empty std::function, so you get bad call when calling it.
what you wanted was the type of the lambda stored in it, you should define that with static constexpr auto, because you are not allowed to know the type of the lambda.
class Processor {
private:
static constexpr auto _lenComp = [](string a, string b) { return a != b && a.size() >= b.size(); };
set<string, decltype(_lenComp)> _inputSet;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With