C++ 就位施工中的回位失败
我有一个我想要构造一次的对象,因为它所在的类通过向对象添加原始指针来跟踪它的对象。尽管如此,以内联方式构建它似乎失败了:C++ 就位施工中的回位失败,c++,constructor,c++14,move-constructor,in-place,C++,Constructor,C++14,Move Constructor,In Place,我有一个我想要构造一次的对象,因为它所在的类通过向对象添加原始指针来跟踪它的对象。尽管如此,以内联方式构建它似乎失败了: // Defined utilities: ModuleClusterPlot(Type typeArg, const int& layer, const int& module, const int& ladder, const int& startEventArg, const int& endEventArg); ~ModuleC
// Defined utilities:
ModuleClusterPlot(Type typeArg, const int& layer, const int& module, const int& ladder, const int& startEventArg, const int& endEventArg);
~ModuleClusterPlot();
// Invalid utilities
ModuleClusterPlot(ModuleClusterPlot& t_other) = delete;
ModuleClusterPlot(ModuleClusterPlot&& t_other) = delete;
ModuleClusterPlot& operator=(const ModuleClusterPlot& t_other) = delete;
ModuleClusterPlot& operator=(ModuleClusterPlot&& t_other) = delete;
通过emplace back调用构造函数失败,因为它试图调用移动构造函数(为什么?)
我做错了什么?我使用的是带有std=c++14
标志的gcc7.1.0
最简单的例子:
#include <vector>
class ModuleClusterPlot
{
public:
enum Type
{
foo = 0,
bar
};
ModuleClusterPlot(Type typeArg);
~ModuleClusterPlot();
// Invalid utilities
ModuleClusterPlot(ModuleClusterPlot& t_other) = delete;
ModuleClusterPlot(ModuleClusterPlot&& t_other) = delete;
ModuleClusterPlot& operator=(const ModuleClusterPlot& t_other) = delete;
ModuleClusterPlot& operator=(ModuleClusterPlot&& t_other) = delete;
};
int main()
{
std::vector<ModuleClusterPlot> collection;
collection.emplace_back(ModuleClusterPlot::foo);
}
#包括
类ModuleClusterPlot
{
公众:
枚举类型
{
foo=0,
酒吧
};
ModuleClusterPlot(类型为arg);
~ModuleClusterPlot();
//无效的实用程序
ModuleClusterPlot(ModuleClusterPlot&t_other)=删除;
ModuleClusterPlot(ModuleClusterPlot&t_other)=删除;
ModuleClusterPlot&operator=(常量ModuleClusterPlot&t_other)=删除;
ModuleClusterPlot&operator=(ModuleClusterPlot&t_其他)=删除;
};
int main()
{
病媒采集;
集合。放置_back(ModuleClusterPlot::foo);
}
如何防止在此处调用移动构造函数?您违反了
emaplce\u back
的约束。如果我们从[sequence.reqmts]中查看表101,我们得到
要求:T应可从ARG安装到X中对于矢量,T也应可插入X中。
重点矿山
由于您的类不可移动插入,因此它将无法与emplace\u back
一起使用
之所以需要这样做,是因为size()
大于capacity()
那么向量需要分配新存储并将元素移动到新存储。如果它不能做到这一点,那么向量就不能按预期工作。std::vector::emplace\u back
需要移动构造函数或复制构造函数。原因是它可能需要重新分配内存,并将现有对象移动/复制到新的缓冲区中
即使只在空向量上调用它,实际上不需要移动任何现有对象,请记住相同的函数
emplace\u back
可用于空向量和非空向量。函数不可能仅从空状态知道正在使用它,因此当成员函数实例化时,处理非空向量的代码也必须有效。您能将其设置为a吗?您编写代码时希望添加原始指针
,但如果收到需要复制/移动构造函数的错误消息,我猜你是想插入一个对象,而不仅仅是指针@ThomasSparber我正在向包含指针的静态对象添加这个。@AdamHunyadi好的,请显示代码并以NathanOliver的身份发布MCVEsuggests@ThomasSparber我会这样做,我的问题更一般,我想阻止emplace返回调用移动构造函数。然后我会将容器更改为链表,谢谢你解释为什么它需要移动构造!
#include <vector>
class ModuleClusterPlot
{
public:
enum Type
{
foo = 0,
bar
};
ModuleClusterPlot(Type typeArg);
~ModuleClusterPlot();
// Invalid utilities
ModuleClusterPlot(ModuleClusterPlot& t_other) = delete;
ModuleClusterPlot(ModuleClusterPlot&& t_other) = delete;
ModuleClusterPlot& operator=(const ModuleClusterPlot& t_other) = delete;
ModuleClusterPlot& operator=(ModuleClusterPlot&& t_other) = delete;
};
int main()
{
std::vector<ModuleClusterPlot> collection;
collection.emplace_back(ModuleClusterPlot::foo);
}