c++ - 是否有关于 std::move 的这种使用的编译警告?

标签 c++ clang llvm c++builder

以下程序显示了 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/

相关文章:

c++ - 如何在不删除任何字符的情况下将字符从字符串中的某个位置移动到它的最前面?

c++ - 如何为文本文件依赖配置 distcc

c++ - Microsoft Visual C++ 的 OpenMP 更新,仍然停留在版本 2 上

llvm 内联 channel 不起作用

C++ std vector 内容范围

c++ - 如果我确定它在只读内存区域中,那么在不重新分配内存的情况下复制 const char ptr 是否被认为是不好的做法?

c++ - clang: 警告: -lgtest: 'linker' 输入未使用

gcc - llvm-gcc 汇编器 : LDR syntax

arm - 用 LLVM 解释 ARM/MachO 进行分析和优化?

c++ - 这种分配内存的方式不好吗?