samedi 27 août 2016

Can't make the decltype specifier work properly inside the lambda function

This piece of code doesn't complie .The compiler remind me that :cannot convert from “initializer list” to “std::priority_queue<int, std::vector<_Ty, std::allocator<_Ty>>, std::less<_Ty>> &”.

    #include <vector>
    #include <queue>

    int main()
    {
        using namespace std;
        priority_queue<int> que;
        auto func = [&]()
        {
            vector<int> vec;
            que = decltype(que)(vec.begin(),vec.end());
            //cannot convert from“initializer list”to“std::priority_queue<int, std::vector<_Ty, std::allocator<_Ty>>, std::less<_Ty>> &”
        };
        func();
        return 0;
    }

If I move the priority_queue declaration into the lambda function ,it compiles perfectly .

#include <vector>
#include <queue>

int main()
{
    using namespace std;
    auto func = [&]()
    {
        priority_queue<int> que;
        vector<int> vec;
        que = decltype(que)(vec.begin(),vec.end());
    };
    func();
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire