how failed constructor roll over to destroy the completed objects?
I know that when a constructor fails, the completed member objects will be destroyed. There is no memory leak.
My question is that how does compiler do that? How can 开发者_C百科compiler know what member is constructed? Does it make any record of it? Does the compiler really destroy everything in this case? How does it guarantee this?
How the compiler does that is up to the compiler.
But yes, you are guaranteed that any constructed objects will be destructed (in the reverse order they were constructed). §15.2/2:
An object that is partially constructed or partially destroyed will have destructors executed for all of its fully constructed subobjects, that is, for subobjects for which the constructor has completed execution and the destructor has not yet begun execution. Should a constructor for an element of an automatic array throw an exception, only the constructed elements of that array will be destroyed. If the object or array was allocated in a new-expression, the matching deallocation function (3.7.3.2, 5.3.4, 12.5), if any, is called to free the storage occupied by the object.
精彩评论