C++ 删除函数 unique_ptr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21943569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:53:24  来源:igfitidea点击:

Deleted function unique_ptr

c++c++11

提问by Patrick.SE

I don't understand the error output at all, I wrote a single class that generates it.

我根本不明白错误输出,我写了一个生成它的类。

UserQueues.h

用户队列.h

#ifndef USERQUEUES_H
#define USERQUEUES_H

#include <deque>
#include <vector>
#include <memory>
#include "Job.h"

class UserQueues 
{
    public:
        typedef std::unique_ptr<Job> JobPtr;
        typedef std::deque<JobPtr> JobDeque;

    public:
        UserQueues();
        void printDeques();
        void addToDeque(JobPtr job, int queueId);

    public:
        const uint QUEUE_QTY = 3;

    private:
        std::vector<JobDeque> _jobDeques;
};

#endif

UserQueues.cpp

用户队列.cpp

#include <iostream>
#include "UserQueues.h"

UserQueues::UserQueues() : 
    _jobDeques()
{
    for(unsigned int idx = 0 ; idx < QUEUE_QTY ; ++idx)
    {
        _jobDeques.push_back(JobDeque());
    }
}

void UserQueues::printDeques()
{
    for (std::size_t idx = 0; idx < _jobDeques.size(); ++idx)
    {
        std::cout << "[";

        for(std::size_t jdx = 0 ; jdx < _jobDeques[idx].size() ; ++jdx)
        {
            std::cout << _jobDeques[idx].at(jdx)->getArrivalTime();

            if(jdx != (_jobDeques[idx].size() - 1))
            {
                std::cout << ", ";
            }
        }
        std::cout << "]" << std::endl;
    } 
}

void UserQueues::addToDeque(JobPtr job, int queueId)
{
    _jobDeques[0].push_front(std::move(job));
}

Output :

输出 :

09:31:34 **** Incremental Build of configuration Debug for project etsmtl-log710-lab02 ****
make all 
make: Warning: File `src/Job.d' has modification time 1.7e+04 s in the future
Building file: ../src/Job.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I"/home/patrickz/git/etsmtl-log710-lab02/include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Job.d" -MT"src/Job.d" -o "src/Job.o" "../src/Job.cpp"
Finished building: ../src/Job.cpp

Building file: ../src/UserQueues.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I"/home/patrickz/git/etsmtl-log710-lab02/include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/UserQueues.d" -MT"src/UserQueues.d" -o "src/UserQueues.o" "../src/UserQueues.cpp"
In file included from /usr/include/c++/4.7/deque:63:0,
                 from /home/patrickz/git/etsmtl-log710-lab02/include/UserQueues.h:4,
                 from ../src/UserQueues.cpp:2:
/usr/include/c++/4.7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<Job>; _Args = {const std::unique_ptr<Job, std::default_delete<Job> >&}]':
/usr/include/c++/4.7/bits/stl_uninitialized.h:77:3:   required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<std::unique_ptr<Job>, const std::unique_ptr<Job>&, const std::unique_ptr<Job>*>; _ForwardIterator = std::_Deque_iterator<std::unique_ptr<Job>, std::unique_ptr<Job>&, std::unique_ptr<Job>*>; bool _TrivialValueTypes = false]'
/usr/include/c++/4.7/bits/stl_uninitialized.h:119:41:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<std::unique_ptr<Job>, const std::unique_ptr<Job>&, const std::unique_ptr<Job>*>; _ForwardIterator = std::_Deque_iterator<std::unique_ptr<Job>, std::unique_ptr<Job>&, std::unique_ptr<Job>*>]'
/usr/include/c++/4.7/bits/stl_uninitialized.h:260:63:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::_Deque_iterator<std::unique_ptr<Job>, const std::unique_ptr<Job>&, const std::unique_ptr<Job>*>; _ForwardIterator = std::_Deque_iterator<std::unique_ptr<Job>, std::unique_ptr<Job>&, std::unique_ptr<Job>*>; _Tp = std::unique_ptr<Job>]'
/usr/include/c++/4.7/bits/stl_deque.h:841:9:   required from ‘std::deque<_Tp, _Alloc>::deque(const std::deque<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<Job>; _Alloc = std::allocator<std::unique_ptr<Job> >; std::deque<_Tp, _Alloc> = std::deque<std::unique_ptr<Job> >]'
/usr/include/c++/4.7/bits/stl_construct.h:77:7:   required from ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::deque<std::unique_ptr<Job> >; _Args = {std::deque<std::unique_ptr<Job, std::default_delete<Job> >, std::allocator<std::unique_ptr<Job, std::default_delete<Job> > > >&}]'
/usr/include/c++/4.7/bits/stl_uninitialized.h:77:3:   [ skipping 2 instantiation contexts ]
/usr/include/c++/4.7/bits/stl_uninitialized.h:260:63:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::deque<std::unique_ptr<Job> >*; _ForwardIterator = std::deque<std::unique_ptr<Job> >*; _Tp = std::deque<std::unique_ptr<Job> >]'
/usr/include/c++/4.7/bits/stl_uninitialized.h:283:69:   required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = std::deque<std::unique_ptr<Job> >*; _ForwardIterator = std::deque<std::unique_ptr<Job> >*; _Allocator = std::allocator<std::deque<std::unique_ptr<Job> > >]'
/usr/include/c++/4.7/bits/vector.tcc:410:6:   required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {std::deque<std::unique_ptr<Job, std::default_delete<Job> >, std::allocator<std::unique_ptr<Job, std::default_delete<Job> > > >}; _Tp = std::deque<std::unique_ptr<Job> >; _Alloc = std::allocator<std::deque<std::unique_ptr<Job> > >]'
/usr/include/c++/4.7/bits/vector.tcc:102:4:   required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::deque<std::unique_ptr<Job, std::default_delete<Job> >, std::allocator<std::unique_ptr<Job, std::default_delete<Job> > > >}; _Tp = std::deque<std::unique_ptr<Job> >; _Alloc = std::allocator<std::deque<std::unique_ptr<Job> > >]'
/usr/include/c++/4.7/bits/stl_vector.h:900:9:   required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::deque<std::unique_ptr<Job> >; _Alloc = std::allocator<std::deque<std::unique_ptr<Job> > >; std::vector<_Tp, _Alloc>::value_type = std::deque<std::unique_ptr<Job> >]'
../src/UserQueues.cpp:9:40:   required from here
/usr/include/c++/4.7/bits/stl_construct.h:77:7: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Job; _Dp = std::default_delete<Job>; std::unique_ptr<_Tp, _Dp> = std::unique_ptr<Job>]'
In file included from /usr/include/c++/4.7/memory:86:0,
                 from /home/patrickz/git/etsmtl-log710-lab02/include/UserQueues.h:6,
                 from ../src/UserQueues.cpp:2:
/usr/include/c++/4.7/bits/unique_ptr.h:262:7: error: declared here
make: *** [src/UserQueues.o] Error 1

09:31:36 Build Finished (took 2s.52ms)

采纳答案by Bret Kuhns

You aren't explicitly following the "rule of three" (or is it five these days?). GCC will attempt to generate a copy constructor of UserQueuessince you did not explicitly = delete;the copy constructor. When it does that, it will attempt to copy your std::vector<JobDeque>, but since that contains move-only std::unique_ptr's, it will fail to compile. So, the constructors of your UserQueuesshould look like:

您没有明确遵循“三规则”(或者现在是五规则?)。GCC 将尝试生成一个复制构造函数,UserQueues因为您没有明确地= delete;复制构造函数。当它这样做时,它会尝试复制您的std::vector<JobDeque>,但由于它包含 move-only std::unique_ptr,它将无法编译。所以,你的构造函数UserQueues应该是这样的:

class UserQueues 
{

    public:
        explicit UserQueues();
        UserQueues(const UserQueues&) = delete;
        UserQueues& operator=(const UserQueues&) = delete;
        ~UserQueues() = default;

    [...]

};

To prevent the copy constructors from getting implicitly generated by the compiler. See this related question I asked two years for more info How to declare a vector of unique_ptr's as class data member?

防止复制构造函数被编译器隐式生成。请参阅我问了两年的相关问题如何将 unique_ptr 的向量声明为类数据成员?

回答by ams

The reason you are getting the error is because a copy is taking place. Copying a unique pointer is not allowed because the unique_ptr copy constructor is deleted in the implementation. If you need to pass a unique_ptr, you can do so by using moveor by using a reference.

您收到错误的原因是正在复制。不允许复制唯一指针,因为在实现中删除了 unique_ptr 复制构造函数。如果需要传递 unique_ptr,可以通过使用move或使用引用来实现。

See also

也可以看看