关于std::move的使用,是否有编译警告
Is there a compile warning about this use of std::move?
以下程序显示了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
}
clang整洁的bugprone-use-after-move
检查器支持这种诊断:
bugprone-use-after-move
如果对象在移动后被使用,则发出警告,例如:
std::string str = "Hello, world!n"; std::vector<std::string> messages; messages.emplace_back(std::move(str)); std::cout << str;
最后一行将触发一个警告,即str在已移动。
[…]
使用
移动变量的任何非重新初始化的情况(见下文(都被视为使用。
[…]
如果移动后发生多次使用,则只标记其中的第一次。
clang整洁有一个performance-move-const-arg
检查,它会警告:
- 如果用常量参数调用std::move((
- 如果std::move((是用一个普通可复制类型的参数调用的
- 如果std::move((的结果作为const引用参数传递
在这三种情况下,检查都会建议进行修复,删除std::move((。
,示例如下:
const string s; return std::move(s); // Warning: std::move of the const variable has no effect int x; return std::move(x); // Warning: std::move of the variable of a trivially-copyable type has no effect void f(const string &s); string s; f(std::move(s)); // Warning: passing result of std::move as a const reference argument; no move will actually happen
相关文章:
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 关于std::move的使用,是否有编译警告
- 标准是否严格定义了该程序应该如何编译?
- 是否可以在编译时限制类的实例数?
- 检查编译时是否存在静态函数
- 是否可以编译 Gtk+ 应用程序以同时在 Gtk 2 和 3 上运行
- std::less是否应该允许在编译时比较不相关的指针?
- 编译时检查特征专用化是否具有唯一 ID
- GTK C++:找不到信号处理程序 您是否使用 -rdynamic 进行了编译?
- 是否可以在编译时初始化对象的 C 样式函数指针,以便它调用对象的成员函数?
- 如何确定integer_sequence在编译时是否包含给定的数字?
- 知道模板参数在编译时是否为 const char*?
- 全局变量在 C++ 中是否显着提高了编译速度?
- 检查该类在编译时C++中是否有任何基类
- 如何判断是否在编译时计算了"constexpr"(无需手动检查)
- cMake/cTest:检查代码是否编译
- 我怎么知道编译器是否C++编译时计算表达式
- 在线编译器工具是否执行所有操作,或者它们只是检查是否编译
- constexpr数组成员是否编译时间常量
- 检查表达式是否编译的可移植方法