Does std :: move invalidate pointers indicate?

Assume the following:

template<typename Item>
class Pipeline
{
    [...]

    void connect(OutputSide<Item> first, InputSide<Item> second)
    {
        Queue<Item> queue;
        first.setOutputQueue(&queue);
        second.setInputQueue(&queue);
        queues.push_back(std::move(queue));
    }

    [...]

    std::vector<Queue<Item> > queues;
};

Will the queue of pointers work in the "first" and "second" after moving?

+4
source share
3 answers

Pointers will not work because queue- this is a local object that will be deleted at the end connect. Even using std::move, you still create a new object in a new memory location. He will simply try to use as much as possible from the "old" object.

, std::move, push_back , . , connect .

- queue . ++ 11:

#include <memory>

template<typename Item>
class Pipeline
{
    [...]

    void connect(OutputSide<Item> first, InputSide<Item> second)
    {
        auto queue = std::make_shared<Queue<Item>>();
        first.setOutputQueue(queue);
        second.setInputQueue(queue);
        queues.push_back(queue);
    }

    [...]

    std::vector<std::shared_ptr<Queue<Item>>> queues;
};
+4

std:: move invalidate ?

. , . Queue , ( ); (, ).

"" "" ?

. , ; , - .

, , , . , - , undefined.

, , , queues:

queues.push_back(std::move(queue));
first.setOutputQueue(&queue.back());
second.setInputQueue(&queue.back());

queues , , .

, deque list, . , , () , , Danvil.

+6

, , , move, , . .

move, , . ? , . -construtcor, -.

, "" . "". , std::move. , copy .

, - , , .

, "move" , , , strong > ( , , move).

, , , . , "". .

. queue, , moves queue.

I hope it is now clear what is happening and what you can do with it.

+1
source

Source: https://habr.com/ru/post/1536442/


All Articles