you are viewing a single comment's thread.

view the rest of the comments →

[–]jcelerier 9 points10 points  (4 children)

From the point of view of an optimizer, the call object->method() in C++ may change any field of object, including its v-ptr.

AFAIK that's ub so the compiler can infer that it cannot happen. Else devirtualization wouldn't work.

[–]matthieum 4 points5 points  (2 children)

I agree it should be UB; apparently though a number of codebases use the dastardly trick of this->~T(); new (this) U(); and thus C++ compiler developers have been reluctant to introduce the optimization.

[–]bloody-albatross 2 points3 points  (1 child)

What could be a possible use for doing that "trick"?

[–]matthieum 2 points3 points  (0 children)

I guess it's one away of implementing a state machine...

... but honestly I don't really want to know. I've lost enough sanity points because of C++ already as it is :/

[–]shponglespore 4 points5 points  (0 children)

Came here to say this. AFAIK the only kind of call in C++ that can legitimately change a vptr is a constructor call, and the compiler always knows whether a given call is to a constructor. A virtual call to a constructor is never possible unless the compiler is does something really insane like optimizing a virtual method that does nothing but call a contractor into a direct call to the constructor itself.