【发布时间】:2017-07-13 11:28:39
【问题描述】:
我有一个对象,我想只构造一次,因为它所在的类通过向它们添加原始指针来跟踪它的对象。不过,内联构造它似乎失败了:
// 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 调用构造函数失败,因为它试图调用移动构造函数(为什么?):
moduleClusterPlots.emplace_back(t_type, t_layer, t_module, t_ladder, i, i);
我在这里做错了什么?我正在使用gcc 7.1.0 和std=c++14 标志。
小例子:
#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);
}
如何防止在此处调用移动构造函数?
【问题讨论】:
-
你能把这个设为minimal reproducible example吗?
-
你写你想添加
raw pointers但是如果你得到一个需要复制/移动构造函数的错误消息,我猜你是在尝试插入一个对象而不是指针!跨度> -
@ThomasSparber 我将
this添加到保存指针的静态对象中。 -
@AdamHunyadi 好的,请按照 NathanOliver 的建议显示代码并发布 MCVE
-
@ThomasSparber 我会这样做,不过我的问题更笼统,我想阻止 emplace back 调用移动构造函数。
标签: c++ constructor c++14 move-constructor in-place