mercredi 29 août 2018

unique_ptr reference to deleted function when implementing interface

I have the following code, which is being built using the MSVC2013 toolchain, C++11 (Old stuff a function of work constraints):

template<class T>
class AbstractWrappedQueue {
public:
    virtual bool empty() = 0;
    virtual size_t size() = 0;
    virtual void push(T& value) = 0;
    virtual void push(T&& value) = 0;
    virtual T pop() = 0;
}

template<class T>
class WrappedQueue // : AbstractWrappedQueue<T>  -- UNCOMMENT FOR COMPILE FAILURE
{
private:
    std::queue<T> q;
public:
    WrappedQueue() {}
    ~WrappedQueue() {}
    bool empty() { return q.empty(); }
    size_t size() { return q.size(); }
    void push(T& p) { q.push(p); }
    void push(T&& p) { q.push(std::move(p)); }
    T pop() {
        T r = std::move(q.front());
        q.pop();
        return r;
    }
}

//Google Test excerpt
TEST(WrappedQueueTest, PtrDoesntDie) {
    WrappedQueue<unique_ptr<int>> ptr;
    //...rest of test is irrelevant.
}

As long as WrappedQueue stands alone, the class, and it's functionality, plays with unique_ptrs without a problem. However, should it implement the AbstractWrappedQueue interface (i.e., uncomment the : AbstractWrappedQueue<T> part of the class definition, then I get the following error:

error C2280: 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function.

I would not expect the application of my interface to cause a situation where an attempt to copy a unique_ptr might occur. Why is this occurring? What can be done to prevent it (aside from simply not using the interface)?

Aucun commentaire:

Enregistrer un commentaire