mercredi 30 octobre 2019

Call to lambda is ambiguous despite explicitly stating the return type

An overloaded function should take both functors in, given the type of the lambda is decidable ( castable to an std::function (please correct me if I'm wrong ). The question is: Why is there a compile error below, despite the lambda type being explicitly defined? ( & -> Type {} )

Please note, that for my current solution I need the capture-by-reference, that's why the code contains the logic for it.

The following example describes the problem:

#include <iostream>
#include <string>

#include <functional>

void do_some(std::function<void(int)> thing){
    thing(5);
}

void do_some(std::function<bool(int)> thing){
    if(thing(10)){
        std::cout << "it's true!" <<std::endl;
    }
}

int main()
{
    int local_to_be_modified = 0;
    do_some([&](int in){
        local_to_be_modified = in;
        std::cout << "This is void-" <<std::endl;
    });
    do_some([&](int in) -> bool{ // error: call to 'do_some' is ambiguous
        local_to_be_modified += in;
        std::cout << "This is bool-" <<std::endl;
        return true;
    });
}

Aucun commentaire:

Enregistrer un commentaire