I have a stl or boost container std :: unique_ptr. As it is known, std :: unique_ptr only supports move simatik in connection with which the question arises, how to return the pointer moved to the container earlier?

#include <boost/circular_buffer.hpp> ... using boost::circular_buffer; ... int main() { circular_buffer<unique_ptr<int>> cb(10); std::mutex mut; std::future async = std::async(std::launch::async, [&]() -> void { while (true) { std::this_thread::sleep_for(10ms); std::lock_guard<std::mutex> lock(mut); if (!cb.empty()) { unique_ptr<int> tmp = cb.back(); // Как тут сделать так что бы был вызван move конструктор? cout << "[" << *tmp << "]"; cb.pop_back(); } } 1 + 1; }); while (true) { std::lock_guard<std::mutex> lock(mut); unique_ptr<int> tmp = std::make_unique<int>(0); cin >> *tmp; cb.push_front(std::move(tmp)); }; return 0; } 

    1 answer 1

    It is enough to execute move:

     ::std::unique_ptr<int> tmp{::std::move(cb.back())};