gpt4 book ai didi

c++ - 当使用 std::vector 的 emplace_back 或 push_back 创建时,在从仅 move 类型派生的类中定义析构函数会产生编译时错误

转载 作者:行者123 更新时间:2023-12-02 04:13:50 28 4
gpt4 key购买 nike

从以下代码中删除注释会出现编译时错误。看来在派生类中定义析构函数会导致在 emplace_back 中调用复制构造函数

#include <vector>

struct A
{
A() = default;
A( A& ) = delete;
A& operator=( A& ) = delete;
A( A&& ) = default;
A& operator=( A&& ) = default;
~A(){};
};

struct B : public A
{
using A::A;
//~B() = default; //ERROR
};

int main()
{
std::vector< B > list;
for( int ii = 0; ii < 3; ii++ ) { list.emplace_back(); }
return 0;
}

错误是:

In file included from /usr/include/c++/5/vector:62:0,
from a.cpp:1:
/usr/include/c++/5/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = B; _Args = {B}]’:
/usr/include/c++/5/bits/stl_uninitialized.h:75:18: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*; bool _TrivialValueTypes = false]’
/usr/include/c++/5/bits/stl_uninitialized.h:126:15: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*]’
/usr/include/c++/5/bits/stl_uninitialized.h:281:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<B*>; _ForwardIterator = B*; _Tp = B]’
/usr/include/c++/5/bits/stl_uninitialized.h:303:2: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = B*; _ForwardIterator = B*; _Allocator = std::allocator<B>]’
/usr/include/c++/5/bits/vector.tcc:422:8: required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {}; _Tp = B; _Alloc = std::allocator<B>]’
/usr/include/c++/5/bits/vector.tcc:101:23: required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {}; _Tp = B; _Alloc = std::allocator<B>]’
a.cpp:24:57: required from here
/usr/include/c++/5/bits/stl_construct.h:75:7: error: invalid initialization of non-const reference of type ‘B&’ from an rvalue of type ‘B’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
a.cpp:15:8: note: initializing argument 1 of ‘B::B(B&)’
struct B : public A
^

我使用像 A 这样的基类来管理句柄,并且希望在派生类中定义析构函数主要用于调试目的。目前我在 vector 中使用智能指针来避免这个问题。

我想知道是什么导致了这个问题,以及如何通过更改代码或使用更合适的容器来解决这个问题。如有任何帮助,我们将不胜感激。

编辑:我正在使用 g++ -std=c++11 g++ 版本 g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609

进行编译

最佳答案

使用static_assert验证您对CopyConstructibleMoveConstructible的期望:

static_assert(!std::is_copy_constructible<A>{});
static_assert( std::is_move_constructible<A>{});

static_assert(!std::is_copy_constructible<B>{});
static_assert(!std::is_move_constructible<B>{});

声明~B() 时,编译器会隐式删除B(B&&)。您可以使用显式声明覆盖该行为:

B(B&&) = default;

关于c++ - 当使用 std::vector 的 emplace_back 或 push_back 创建时,在从仅 move 类型派生的类中定义析构函数会产生编译时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58313096/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com