- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从以下代码中删除注释会出现编译时错误。看来在派生类中定义析构函数会导致在 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
验证您对CopyConstructible和MoveConstructible的期望:
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/
我正在尝试创建执行搜索的线程 vector 。这是我的 SearchThread 类中的重要内容。 class SearchThread { explicit SearchThread
给定以下代码 测试.h: #ifndef __graph_aufbau_header__ #define __graph_aufbau_header__ #include #include #in
关于 someFunctionOne 和 someFunctionTwo。 当每个都放置到 map 中时,最终结果是否仍然相同? 指针是否升级为智能指针? struct MyStruct {
我对 emplace_back 的行为有点困惑。一起来看看吧。 struct innerObject { innerObject(int ); }; class outterObject {
我尝试将类 cBar 的两个实例放置到具有 emplace_back 函数的 vector 中。 根据reference调用 emplace_back 仅保留 vector 中的位置,然后“就地”创建
我想知道是否可以使用 emplace_back 将项目存储到 vector 中,emplace_back 是一种派生自 vector 所期望的类的类型。 例如: struct fruit {
这是我日常工作中的一段代码。我只想问你这两种情况有什么区别,尤其是在性能方面。 std::vector > aVec; // case 1 aVec.emplace_back("hello", "bo
我向你展示我的问题 我有 2 个列表,将它们命名为 A 和 B。 list > A = {{1},{2},{3}}; list > B = {{4},{5},{6}}; 我想要的是 A = {{1,4
#include #include using namespace std; class test{ public: test(){
似乎添加默认构造函数会阻止调用 emplace_back 并产生错误消息:“静态断言失败:类型不可分配”(gcc 5.3 with -std=c++14)。这是一个说明问题的简单代码: class A
我编写了一个简单的程序来尝试在标准库容器中就地创建对象。这是我写的: #include #include class AB { public: explicit AB(int n);
我想知道我是否正确理解emplace_back #include using namespace std; struct Hero { Hero(const string&) {}
这个问题在这里已经有了答案: Emplace an aggregate in std::vector (3 个答案) 关闭 4 年前。 为什么我可以直接调用构造函数,但是当我在 emplace_ba
我正在尝试为我的用户定义结构使用 emplace_back: #include #include #include struct IDNumber { IDNumber(std::vec
考虑下面的例子: #include #include class S { public: S() { puts("S()"); } S(int) { puts("S(int)"); }
我正在创建一个指向新对象的新指针,并立即将 push_front 放入双端队列。我想改为使用 emplace_front,但遇到编译器错误。 我的对象构造函数需要 1 个字符串参数。 std::deq
所以我一直在制作一个对象池类,它是这样使用的: class MagicTrick { public: MagicTrick(int magic) : _magic(magic) {}
#include #include struct T{ T(){ std::cout vec; vec.push_back(T()); vec.push_
我在实现我自己的模板类时遇到了困难,我想在其中添加我自己的 emplace_back 函数实现。由于我仍在学习模板设计,非常感谢您在这里提供意见。 template class MydataStru
这个问题在这里已经有了答案: emplace_back() does not behave as expected (2 个答案) 关闭 8 年前。 我很好奇这段代码的输出结果。 #include
我是一名优秀的程序员,十分优秀!