Skip to main content

Why do I get a C26486 compiler error moving a deque to a vector? RRS feed

  • Question

  • I don't know why I get a C26486 "Don't pass a pointer that may be invalid to function" when I copy or move the items using an iterator in a deque or list into a vector, but a constant_iterator works. Additionally, using an iterator works from a vector. Here is some example code:

    #include <vector>
    #include <deque>
    #include <list>
    int main()
        std::deque<int> d_src;
        std::vector<int> dest{ d_src.begin(),d_src.end() };  // C26486
        std::vector<int> dest_c{ d_src.cbegin(),d_src.cend() };
        std::deque<int> dest2{ d_src.begin(),d_src.end() }; // C26486
        std::vector<int> v_src;
        std::vector<int> v_dest{ v_src.begin(),v_src.end() };
        std::deque<int> v_dest2{ v_src.begin(),v_src.end() };
        std::list<int> v_dest3{ v_src.begin(),v_src.end() };
        std::list<int> l_src;
        std::vector<int> l_dest{ l_src.begin(),l_src.end() };   // C26486
        std::vector<int> l_dest_c{ l_src.cbegin(),l_src.cend() };

    I am using Visual Studio 2019 Community 16.2.5, Visual C++ 2019 00435-60000-00000-AA505 using the c++17 language standard. I have enabled Code Analysis on build, and I have selected "Microsoft All Rules."

    If someone could explain the concept about lifetimes that I'm missing, I would appreciate it.

    P.S. - I get the same error with these alternatives

        std::deque<token> toks_;
        std::vector<token> ret;
    // Alternative 1
        for(auto it= toks_.begin();it != toks_.end();++it)
    // Alternative 2
        for (auto&& tok : toks_) ret.emplace_back(tok);
    // Alternative 3
        std::move(toks_.begin(), toks_.end(), std::back_inserter(ret));

    Friday, September 13, 2019 6:21 PM

All replies