std :: move-如何警告程序员不要使用*从*对象移动

gau*_*waj 7 move-semantics c++11 c++14

假设有这样的函数:

int * func()
{
  std::unique_ptr<int> ptr(new int(3));
  //Few more lines of code

  //then one function added where programmer writes like some thing

  SOME_OTHER_FUNC(std::move(ptr));

  return ptr.get();
}



void SOME_OTHER_FUNC(std::unique_ptr<int> arg_ptr)
{
}
Run Code Online (Sandbox Code Playgroud)

有没有办法警告程序员避免这样的错误std::move?这unique_ptr不仅仅是针对其他对象而已.

当我们不恰当地使用移动对象时,是否有任何机制来生成警告?

Seb*_*edl 15

std::move 警告.如果您的程序员不理解这一点,您必须更好地教育他们.如果函数太长以至于程序员可以合理地忽略移动,那么你需要重构你的函数以缩短它.

  • 或者,从变量中限制`move`'d的范围,因此它的生命周期结束和开始就在它移动的位置旁边. (2认同)