Yes, delete this; has defined results, as long as (as you've noted) you assure the object was allocated dynamically, and (of course) never attempt to use the object after it's destroyed. Over the years, many questions have been asked about what the standard says specifically about delete this;, as opposed to deleting some other pointer. The answer to that is fairly short and simple: it doesn't say much of anything. It just says that delete's operand must be an expression that designates a pointer to an object, or an array of objects. It goes into quite a bit of detail about things like how it figures out what (if any) deallocation function to call to release the memory, but the entire section on delete (§[expr.delete]) doesn't mention delete this; specifically at all. The section on destructors does mention delete this in one place (§[class.dtor]/13):
在定义虚析构函数时(包括隐式定义(15.8)),非数组释放函数的确定就像表达式delete this出现在析构函数类的非虚析构函数中一样(参见8.3.5)。
这倾向于支持标准考虑删除这个的想法;如果它是无效的,它的类型就没有意义。这是标准中唯一提到删除的地方;据我所知,根本没有。
不管怎样,有些人认为删除这条消息是一种恶意攻击,并告诉任何愿意听的人应该避免这样做。一个经常被引用的问题是很难确保类的对象只被动态分配。其他人认为这是一个非常合理的习语,并一直在使用它。就我个人而言,我介于两者之间:我很少使用它,但当它似乎是适合这项工作的工具时,不要犹豫。
The primary time you use this technique is with an object that has a life that's almost entirely its own. One example James Kanze has cited was a billing/tracking system he worked on for a phone company. When you start to make a phone call, something takes note of that and creates a phone_call object. From that point onward, the phone_call object handles the details of the phone call (making a connection when you dial, adding an entry to the database to say when the call started, possibly connect more people if you do a conference call, etc.) When the last people on the call hang up, the phone_call object does its final book-keeping (e.g., adds an entry to the database to say when you hung up, so they can compute how long your call was) and then destroys itself. The lifetime of the phone_call object is based on when the first person starts the call and when the last people leave the call -- from the viewpoint of the rest of the system, it's basically entirely arbitrary, so you can't tie it to any lexical scope in the code, or anything on that order.
对于任何可能关心这种编码的可靠性的人来说:如果你打电话到欧洲的任何地方,从欧洲的任何地方,或通过欧洲的任何地方,很有可能它是由代码处理的(至少是部分)。