mercredi 20 février 2019

Why does this incorrect std::function initialization compile using MSVC?

Came across an interesting issue today started by my own typo. I created a lambda that takes in a reference to a struct and incorrectly set it to a std::function that receives it's argument by value.

Here's a more concise version:

#include <functional>

struct InputStruct
{
    int i;
    InputStruct(): i(1){}
};

void function_rcv(std::function<bool(InputStruct)> & func_ref)
{
    InputStruct in;
    func_ref(in);
}


int main()
{
    std::function<bool(InputStruct)> my_func = [](InputStruct & in)->bool{return in.i==1;};
    function_rcv(my_func);
}

Checking with godbolt shows this compiles successfully with MSVC, but fails for both Clang and GCC.

Interestingly enough, using a primitive instead of a struct fails compilation on all three compilers.

Is this a bug in the MSVC compiler?

Aucun commentaire:

Enregistrer un commentaire