在std :: move之后使用对象不会导致编译错误 [英] Using an object after std::move doesn't result in a compilation error

查看:53
本文介绍了在std :: move之后使用对象不会导致编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在对象上调用 std :: move 后,如果此后使用该对象,为什么语言不引起编译错误?

After std::move is called on an object, why doesn't the language cause a compilation error if the object is used after?

是因为编译器无法检测到这种情况吗?

Is it because it is not possible for compiler to detect this condition?

推荐答案

C ++语言设计的一般原则是信任程序员".在将对象作为 std :: move 的参数之后,拒绝使用该对象时,我会想到一些问题.

The general principle in C++ language design is "trust the programmer". Some issues I can think of with rejecting any use of the object after it has been an argument to std::move.

  • 通常情况下,确定给定用途是否在调用 std :: move 之后,就等于解决了暂停问题.(换句话说,这不可能完成.)您将不得不提出一些规则,这些规则以可以静态确定的方式描述之后"的含义.
  • 通常,将一个对象分配给 std :: move 的参数是绝对安全的.(一个特定的类可能会引起一个断言,但这将是一个非常奇怪的设计.)
  • 对于编译器来说,很难判断给定的函数是否只是将新值分配给类的元素.
  • To determine whether a given use is after a call to std::move in the general case would be equivalent to solving the halting problem. (In other words, it can't be done.) You would have to come up with some rules that describe what you mean by "after" in a way that can be statically determined.
  • In general, it is perfectly safe to assign to an object that has been an argument to std::move. (A particular class might cause an assertion, but that would be a very odd design.)
  • It's hard for a compiler to tell whether a given function is going to just assign the elements of the class new values.

这篇关于在std :: move之后使用对象不会导致编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆