简体   繁体   English

析构函数是移动构造函数/赋值的 RHS 上唯一调用的吗?

[英]Is the destructor the only thing ever called on the RHS of a move ctor/assignment?

My search-fu is good, but this is a difficult one to phrase correctly to find the answer.我的搜索功能很好,但这很难用正确的措辞找到答案。 Basically, after a move ctor/assignment is invoked, is it guaranteed that the only thing that will ever be called on the RHS will be the destructor?基本上,在调用移动构造函数/赋值之后,是否可以保证在 RHS 上唯一调用的将是析构函数?

The reason I ask is that I have various things that (for sanity's sake) cannot be in an invalid state.我问的原因是我有各种各样的东西(为了理智)不能处于无效状态。 But, far and away the most efficient move scheme would be to swap some stuff into them that the dtor can accept but nothing else could.但是,最有效的移动方案是将一些 dtor 可以接受但没有其他东西可以接受的东西交换到它们中。 Else, I have to allocate actual data, no matter how trivial, to keep the RHS in a valid state.否则,我必须分配实际数据,无论多么微不足道,以保持 RHS 处于有效状态。

If the dtor is the only thing that will ever get called, then I can get maximum efficiency.如果 dtor 是唯一会被调用的东西,那么我可以获得最大的效率。

is it guaranteed that the only thing that will ever be called on the RHS will be the destructor?是否保证在 RHS 上唯一调用的将是析构函数?

No. It is well-formed to call member functions of a moved from object.不。调用移动自对象的成员函数是格式良好的。 The standard doesn't guarantee that a programmer won't do that.该标准并不能保证程序员不会这样做。

As the implementor of a class, you can decide that some member functions must not be called on a moved from object, and thus can avoid for example allocating memory.作为类的实现者,您可以决定某些成员函数不得在移动自对象上调用,从而可以避免例如分配内存。 Or, you can decide to not have such requirement.或者,您可以决定没有这样的要求。 In general, having preconditions can allow more efficient implementation, while not having preconditions makes the class easier to use.一般来说,拥有前置条件可以实现更高效的实现,而没有前置条件则使类更易于使用。

As the user of a class, you are responsible for following the preconditions of the member functions that you call (or member access).作为类的用户,您有责任遵守您调用(或成员访问)的成员函数的先决条件。 If a precondition of a function is that class is not in a moved from state, then don't break that precondition.如果函数的先决条件是该类未处于移动状态,则不要破坏该先决条件。

As a general rule, it's probably a good design to allow assignment operator to be called on a moved from object.作为一般规则,允许在移动的对象上调用赋值运算符可能是一个很好的设计。 That's what all (assignable) standard library classes do.这就是所有(可分配的)标准库类所做的。


In short: There is no such guarantee by the standard, but you can impose such requirement on the user of the class.简而言之:标准没有这样的保证,但是您可以对类的用户施加这样的要求。 Just make sure it is well documented.只要确保它有据可查。

There's nothing magic about moving from an object.从一个物体上移动并没有什么神奇之处。 After a move the object is still valid and if it's not a temporary your code can call member functions on it and pass it to functions.移动后,对象仍然有效,如果它不是临时对象,您的代码可以调用其上的成员函数并将其传递给函数。

Just like any other object, and this is really the point of the question, the compiler won't do anything to an object other than destroy it at the end of its lifetime.就像任何其他对象一样,这确实是问题的重点,编译器除了在生命周期结束时销毁它外,不会对它做任何事情。 For a temporary object, that's the end of the full statement that creates the object.对于临时对象,这是创建对象的完整语句的结尾。 For a named object, that's the end of the scope in which the object is created.对于命名对象,这是创建对象的范围的结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM