<tfoot id='MDaQB'></tfoot>
    <bdo id='MDaQB'></bdo><ul id='MDaQB'></ul>

<small id='MDaQB'></small><noframes id='MDaQB'>

    <i id='MDaQB'><tr id='MDaQB'><dt id='MDaQB'><q id='MDaQB'><span id='MDaQB'><b id='MDaQB'><form id='MDaQB'><ins id='MDaQB'></ins><ul id='MDaQB'></ul><sub id='MDaQB'></sub></form><legend id='MDaQB'></legend><bdo id='MDaQB'><pre id='MDaQB'><center id='MDaQB'></center></pre></bdo></b><th id='MDaQB'></th></span></q></dt></tr></i><div id='MDaQB'><tfoot id='MDaQB'></tfoot><dl id='MDaQB'><fieldset id='MDaQB'></fieldset></dl></div>
    <legend id='MDaQB'><style id='MDaQB'><dir id='MDaQB'><q id='MDaQB'></q></dir></style></legend>

    1. 插入具有没有复制构造函数的对象的向量

      时间:2023-10-07
          <tbody id='WX2Wj'></tbody>

      • <small id='WX2Wj'></small><noframes id='WX2Wj'>

            <legend id='WX2Wj'><style id='WX2Wj'><dir id='WX2Wj'><q id='WX2Wj'></q></dir></style></legend>

          1. <i id='WX2Wj'><tr id='WX2Wj'><dt id='WX2Wj'><q id='WX2Wj'><span id='WX2Wj'><b id='WX2Wj'><form id='WX2Wj'><ins id='WX2Wj'></ins><ul id='WX2Wj'></ul><sub id='WX2Wj'></sub></form><legend id='WX2Wj'></legend><bdo id='WX2Wj'><pre id='WX2Wj'><center id='WX2Wj'></center></pre></bdo></b><th id='WX2Wj'></th></span></q></dt></tr></i><div id='WX2Wj'><tfoot id='WX2Wj'></tfoot><dl id='WX2Wj'><fieldset id='WX2Wj'></fieldset></dl></div>
            • <bdo id='WX2Wj'></bdo><ul id='WX2Wj'></ul>
              • <tfoot id='WX2Wj'></tfoot>
                本文介绍了插入具有没有复制构造函数的对象的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个类的拷贝构造函数被显式删除(因为 A 在内部使用指针,我不想陷入浅拷贝陷阱):

                class A {上市:A(const A&) = 删除;A&运算符=(const A&) = 删除;A(const B& b, const C& c);}

                现在我有一个 vector 类型的向量.aVector; 并且我想在其中插入元素 - 所以我使用 emplace_back:

                aVector.emplace_back(b, c);

                但是,这无法使用 gcc 进行编译,并且出现错误 -

                third-party/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/stl_construct.h:在'的实例化中void std::_Construct(_T1*, _Args&&...)第三方/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/stl_uninitialized.h:77:3: 需要来自'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator)第三方/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/stl_uninitialized.h:119:41:来自'_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator)第三方/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/stl_uninitialized.h:260:63: 需要来自'_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&)第三方/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/stl_uninitialized.h:283:67:来自'_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&)第三方/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/vector.tcc:410:6: 'void std 要求::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...)第三方/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/vector.tcc:102:4: 'void std: 要求::vector<_Tp, _Alloc>::emplace_back(_Args&& ...)

                这个错误的原因是什么,如何在不删除复制构造函数的情况下修复它?我是否需要移动构造函数 - 是否需要明确定义?

                解决方案

                您应该添加移动构造函数 - 因为 std::vector::emplace_back 可能会执行需要复制/移动构造函数的重定位.或者只是使用 std::deque.

                现场演示

                #include <向量>#include <deque>使用命名空间标准;结构 NoCopyNoMove{NoCopyNoMove(const NoCopyNoMove&) = 删除;NoCopyNoMove&运算符=(const NoCopyNoMove&)=删除;NoCopyNoMove(NoCopyNoMove&&) = 删除;NoCopyNoMove&运算符=(NoCopyNoMove&&)=删除;NoCopyNoMove(int){};};结构仅移动{OnlyMove(const OnlyMove&) = delete;OnlyMove&运算符=(const OnlyMove&)=删除;OnlyMove(OnlyMove&&) noexcept {}OnlyMove&operator=(OnlyMove&&) noexcept {}OnlyMove(int){};};int main(){双端队列X;x.emplace_back(1);矢量y;y.emplace_back(1);}

                <小时><块引用>

                § 23.2.3 表 101 — 可选的序列容器操作

                a.emplace_back(args) [...]

                要求:T 应该是 EmplaceConstructible 从 args 到 X 的.对于vectorT 也应该是MoveInsertableX.

                I have a class whose copy constructors are explicitly deleted (because A uses pointers internally and I don't want to fall into shallow copy pitfalls):

                class A {
                  public:
                    A(const A&) = delete;
                    A& operator=(const A&) = delete;
                
                    A(const B& b, const C& c);
                }
                

                Now I have a vector of type vector<A> aVector; and I want to insert elements into it - so I use emplace_back:

                aVector.emplace_back(b, c);
                

                However, this fails to compile using gcc and I get the error -

                third-party/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...)
                third-party/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/stl_uninitialized.h:77:3:   required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator)  
                third-party/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/stl_uninitialized.h:119:41:   required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) 
                third-party/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/stl_uninitialized.h:260:63:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) 
                third-party/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/stl_uninitialized.h:283:67:   required from '_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&)
                third-party/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/vector.tcc:410:6:   required from 'void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) 
                third-party/gcc-4.7.1-glibc-2.14.1/libgcc/libgcc-4.7.1/afc21dc/include/c++/4.7.1/bits/vector.tcc:102:4:   required from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...)
                

                What is the reason for this error and how can it be fixed without removing the deletion of the copy constructors? Do I need a move constructor - does it need to be explicitly defined?

                解决方案

                You should add move constructor - because std::vector::emplace_back may do relocation which requires copy/move constructor. Or just use std::deque.

                LIVE DEMO

                #include <vector>
                #include <deque>
                using namespace std;
                
                struct NoCopyNoMove
                {
                    NoCopyNoMove(const NoCopyNoMove&) = delete;
                    NoCopyNoMove& operator=(const NoCopyNoMove&) = delete;
                    NoCopyNoMove(NoCopyNoMove&&) = delete;
                    NoCopyNoMove& operator=(NoCopyNoMove&&) = delete;
                
                    NoCopyNoMove(int){};
                };
                
                struct OnlyMove
                {
                    OnlyMove(const OnlyMove&) = delete;
                    OnlyMove& operator=(const OnlyMove&) = delete;
                    OnlyMove(OnlyMove&&) noexcept {}
                    OnlyMove& operator=(OnlyMove&&) noexcept {}
                
                    OnlyMove(int){};
                };
                
                int main()
                {
                    deque<NoCopyNoMove> x;
                    x.emplace_back(1);
                
                    vector<OnlyMove> y;
                    y.emplace_back(1);
                }
                


                § 23.2.3 Table 101 — Optional sequence container operations

                a.emplace_back(args) [...]

                Requires: T shall be EmplaceConstructible into X from args. For vector, T shall also be MoveInsertable into X.

                这篇关于插入具有没有复制构造函数的对象的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:依赖范围;前面需要typename; 下一篇:使用 istream_iterators 构造向量

                相关文章

                最新文章

                <i id='yotwu'><tr id='yotwu'><dt id='yotwu'><q id='yotwu'><span id='yotwu'><b id='yotwu'><form id='yotwu'><ins id='yotwu'></ins><ul id='yotwu'></ul><sub id='yotwu'></sub></form><legend id='yotwu'></legend><bdo id='yotwu'><pre id='yotwu'><center id='yotwu'></center></pre></bdo></b><th id='yotwu'></th></span></q></dt></tr></i><div id='yotwu'><tfoot id='yotwu'></tfoot><dl id='yotwu'><fieldset id='yotwu'></fieldset></dl></div>
              • <tfoot id='yotwu'></tfoot>
                <legend id='yotwu'><style id='yotwu'><dir id='yotwu'><q id='yotwu'></q></dir></style></legend>

                1. <small id='yotwu'></small><noframes id='yotwu'>

                      <bdo id='yotwu'></bdo><ul id='yotwu'></ul>