Why is the code below require ProcessTask
to have copy constructor?
#include <utility>
#include <vector>
template<typename T>
struct ProcessTask
{
using Handle = int*;
ProcessTask() : m_h(nullptr) {}
ProcessTask(Handle handle) noexcept : m_h(handle) {}
ProcessTask(ProcessTask&& other) noexcept : m_h(std::exchange(other.m_h, nullptr)) {}
ProcessTask& operator=(ProcessTask&& other) noexcept
{
free();
m_h = std::exchange(other.m_h, nullptr);
return *this;
}
~ProcessTask()
{
free();
}
void free()
{
delete m_h;
}
Handle m_h;
};
ProcessTask<int> loadData()
{
return {};
}
int main()
{
using Task = ProcessTask<int>;
std::vector<Task> order_tasks
{
loadData(),
loadData()
};
return 0;
}
What is the difference between the initializer list and
order_tasks.push_back(loadData());
that does not require copy constructor?
EDIT1
The same effect with std::unique_ptr
:
std::vector<std::unique_ptr<int>>
{
std::make_unique<int>(5)
};