My code:
template <typename T>//, T value_if_empty>
class AtomicQueue {
std::mutex m;
std::queue<T> q;
T value_if_empty;
public:
AtomicQueue(const T& emptyval) : value_if_empty(std::move(emptyval)) {};
void push(const T& t)
{
m.lock();
q.push(std::move(t));
m.unlock();
}
T pop()
{
T a = std::move(value_if_empty);
m.lock();
if (!q.empty())
{
a = std::move(q.front());
q.pop();
}
m.unlock();
return a;
}
};
AtomicQueue<int> A(0);
AtomicQueue<std::unique_ptr<Class1>> B(nullptr);
AtomicQueue < std::pair<int, std::unique_ptr<Class1>>> C({ 0,nullptr });
This code compiles if I declare just A and B but fails when C is also declared. The error is 'std::pair<int,std::unique_ptr<Class1,std::default_delete<Class1>>>::pair(const std::pair<int,std::unique_ptr<Class1,std::default_delete<Class1>>> &)': attempting to reference a deleted function
The problem seems to be that std::pair<int, std::unique_ptr<Class1>>
does not have a copy constructor. But the same is true for std::unique_ptr<Class1>
. So why does the declaration for B compile but C does not? Also, where exactly is the copy constructor being used? Sorry if the answer is trivial. I am new to using unique_ptr and move constructors.
Side Question: Is there a cleaner or better way to use value_if_empty? I do not want anything other than nullptr being passed when unique_ptr is being used.
Aucun commentaire:
Enregistrer un commentaire