lundi 24 septembre 2018

Am i doing something wrong?

I am running the following code on MSVC 2017 version 15.7.4 and it seems to be crashing at runtime in xutility with the following

Exception thrown: write access violation.
this->_Myproxy was 0xCCCCCCCC.

In function:

inline void _Container_base12::_Swap_all(_Container_base12& _Right) _NOEXCEPT

My code that generates this crash is the following:

std::vector<int>&& MyFunc(int i)
{
    std::vector<int> a;
    a.reserve(10);

    for (int j = 0; j< i; j++)
    {
        a.emplace_back(j);
    }

    return std::move(a);
}

....................

int main()
{
    std::vector<int> b = MyFunc(13);

    for (auto& item : b)
    {
        std::cout << item << " ";
    }
}

Also as a second question: in this scenario would there be any practical difference(excepting the compile time cast to && from the std::move) between writing:

std::vector<int> b = std::move(MyFunc(13)); //or
std::vector<int> b = MyFunc(13);

Thank you!

Aucun commentaire:

Enregistrer un commentaire