Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ bad function call

Im a beginner at c++, i followed some advice on using lambdas instead of a comparator class for my set.

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading