"used after it was moved [bugprone-use-after-move]"警告在这里是一个真正的问题吗?

Is the "used after it was moved [bugprone-use-after-move]" warning a real problem here?

本文关键字:一个 问题 was it after used moved 警告 bugprone-use-after-move 在这里      更新时间:2023-10-16

我正在调整Herb Sutter在2016年cppcon中谈到的deffered ptr的想法,以便能够以更安全的方式管理由id表示的外部资源。

因此,我创建了一个不可复制且只能移动的类,该类包含资源应该表示的id。与unique_ptr一样,如果对象移动到另一个对象,则id应变为0

根据我的理解,即使在被调用的函数没有任何先决条件的情况下,即使在移动了is之后,你仍然应该被允许使用该对象,所以根据我的了解,这应该是有效的:

int main() {
resource src = make_resource(10);
resource dst;
std::cout << "src " << src.get() << std::endl;
std::cout << "dst " << dst.get() << std::endl;
dst = std::move(src);
std::cout << "src " << src.get() << std::endl; // (*)
std::cout << "dst " << dst.get() << std::endl;
src = make_resource(40);
std::cout << "src " << src.get() << std::endl;
std::cout << "dst " << dst.get() << std::endl;
return 0;
}

clang-tidy给了我这个警告:

警告:"src"在移动后使用[移动后使用容易出错]

对于dst = std::move(src)之后的src.get()(上面标记)。

所以我的问题是:

  1. 是否允许我在std::move(src)之后调用src.get()
  2. 我可以假设src.get()std::move之后返回0
  3. 如果1.2.有效,那么有没有办法更改代码,让部落整洁知道这是有效的。如果没有,有没有办法改变代码的有效性

下面是类的实现:

struct resource {
resource() = default;
// no two objects are allowed to have the same id (prevent double free, only 0 is allowed multiple times as it represents nullptr)
resource(const resource&) = delete;
resource& operator=(const resource& other) = delete;
// set the id of the object we move from back to 0 (prevent double free)
resource(resource&& other) noexcept : id(std::exchange(other.id, 0)) {}
resource& operator=(resource&& other) noexcept {
id = std::exchange(other.id, 0);
return *this;
}
// will free the external resource if id not 0
~resource() = default;
// returns the id representing the external resource
int get() const noexcept { return id; }
protected:
// only allow the make function to call the constructor with an id
friend resource make_resource(int id);
explicit resource(int id) : id(id) {}
protected:
int id = 0; // 0 = no resource referenced
};
// in the final version the id should be retrieved by from the external ip
resource make_resource(int id) { return std::move(resource(id)); }
  1. 是否允许在std::move(src)之后调用src.get()

如果我们对src的类型仍然不可知,那么我们就不知道了。可能不会。在某些情况下,在移动后调用成员函数是未定义的。例如,调用智能指针的间接运算符。

给定decltype(src)的定义及其成员函数,我们知道:是的,您可以。

  1. 我可以假设src.get()在std::move之后返回0吗

如果我们对src的类型保持不可知论,那么我们就对src.get()的作用一无所知。更具体地说,我们不知道它的先决条件。

给定decltype(src)的定义及其成员函数:是的,我们可以做出假设。

  1. 如果1。和2。如果是有效的,那么有没有一种方法可以更改代码,让clanicity知道这是有效的。如果没有,有没有办法改变代码的有效性

Clang整洁假设"不处于移出状态"是所有成员函数(除赋值之外)的先决条件,在这种假设下,警告违反了这种假定的先决前提。因此,它试图强制执行一个约定,始终假设这样的预条件,即使您碰巧知道它不存在于您的类中。

您可以在移动和重新分配src之间删除对src.get()的调用。clang整洁不会抱怨的对moved-from变量的一个操作是重新赋值,在赋值之后,对象的状态应该(传统上)得到很好的定义,并且调用其他成员函数被认为是可以的(当然,你可以有其他必须满足的先决条件,但clang整洁可能不知道)虽然从技术上讲,可以定义一种类型,即使在移动后的分配也没有很好地定义,但这种类型是非常非常规和不安全的


总之,您可以在移动后(甚至在重新分配之前)为该特定类调用src.get(),但这样您就不会遵循clang整洁试图强制执行的约定。

cppreference.com有这样的文本:

除非另有说明,否则所有具有已从中移动,处于有效但未指定的状态。也就是说,只有没有先决条件的函数,例如赋值操作员,可以在物体从移动后安全地使用

因此,非正式地说,C++约定是从对象中移出的对象是有效的,但没有用,这就是为什么clang整洁表示使用它是可疑的。

对于您的实施,您提供了"更多"而非常规–所以你的代码没有错,只是非常规的。