【发布时间】:2020-06-07 21:25:32
【问题描述】:
以下程序显示了std::move() 的两个有问题(但在技术上有效)的用法。是否可以使用 LLVM 获得关于这些的编译警告?我注意到在std::move 是多余的一些其他上下文中存在诊断。
我用 bcc32c 版本 5.0.2(基于 LLVM 5.0.2)编译了它,没有收到任何警告。
#include <vector>
int main() {
const std::vector<int> a = {1, 2, 3};
std::vector<int> b = {3, 4, 5};
std::vector<int> c = std::move(a); // std::move from const
std::vector<int> d = std::move(b);
std::vector<int> e = b; // used after std::move
}
【问题讨论】:
-
第二种情况很难诊断。类可以指定移动后的状态是明确定义的;
std::unique_ptr<T>就是一个例子。 -
第二种情况也为
std::vector<T>定义好了。 -
Visual Studio 中的代码分析器(但使用 clang-cl)发现了第二种情况:警告 G8547E8E7:已复制“std::vector”类型的对象“b”[ clang-analyzer-cplusplus.Move] std::vector
e = b; // 在 std::move 之后使用 -
@juanchopanza 定义明确,但可能是无意的。这就是我想要检测它的原因。
-
有趣的问题,我从来没想过。我做了一个MCVE on coliru来说明效果。
标签: c++ clang llvm c++builder