gpt4 book ai didi

c++ - std::vector 无异常:警告 C4530:使用了 C++ 异常处理程序,但未启用展开语义。指定/EHsc

转载 作者:搜寻专家 更新时间:2023-10-31 01:24:41 26 4
gpt4 key购买 nike

<分区>

我正在尝试将一个可移动的、不可复制的对象插入到 std::vector 中。我将原始代码简化为这个例子:

#include <vector>
class MovingClass
{
public:
MovingClass(const int value) : _value(new int(value)) {}
//! No copy allowed
MovingClass(const MovingClass& src) = delete;
MovingClass& operator=(const MovingClass& src) = delete;
// move is OK
MovingClass(MovingClass&& source) : _value(source._value) { source._value = nullptr; }
MovingClass& operator=(MovingClass&& source) { _value = source._value; source._value = nullptr; }

~MovingClass() { delete _value; }
// do not mind that this is all kinds of bad, it's just an example
int* _value;
};

int test_vector_main()
{
std::vector<MovingClass> containers;
containers.push_back({ 42 });

return 0;
}

该类的最初目的是关闭 winapi 句柄,而不是删除指针。

如果我在 Configuration Properties->C/C++->All Options 中将“Enable C++ Exceptions”设置为“No”,我可以在空白项目中重现警告。

我使用的项目将警告视为错误并禁用异常。

除了放弃禁止复制规则或启用异常(exception)之外,我不知道该怎么做。我将启用异常(exception),但如果我不能,我还能做什么?

为什么 push_back 甚至需要异常(exception)?

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