以下程序显示了 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
}
最佳答案
叮叮当当的 bugprone-use-after-move
checker 支持这种诊断:
bugprone-use-after-move
Warns if an object is used after it has been moved, for example:
std::string str = "Hello, world!\n"; std::vector<std::string> messages; messages.emplace_back(std::move(str)); std::cout << str;
The last line will trigger a warning that str is used after it has been moved.
[...]
Use
Any occurrence of the moved variable that is not a reinitialization (see below) is considered to be a use.
[...]
If multiple uses occur after a move, only the first of these is flagged.
关于c++ - 是否有关于 std::move 的这种使用的编译警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60372691/