27 ms·
... but the object has a vtable in the exemple ? a call being optimized because the compiler can infer that a specific call site does not need a vtable, does no
by jcelerier 5y ago
... but the object has a vtable in the exemple ? a call being optimized because the compiler can infer that a specific call site does not need a vtable, does not mean that the vtable isn't there taking up space in the object.
proof: https://godbolt.org/z/W9MMGvTEz https://godbolt.org/z/W9MMGvTEz
proof at runtime (to make sure that the compiler doesn't "lie" about the size, we check the stack address of the variable constructed after the one with a virtual, and after the one without ; in the first case it's 16 bytes after, in the second 1 byte): https://godbolt.org/z/s11o9do4n https://godbolt.org/z/s11o9do4n ; basically in the simplest indirect measure possible that we can make of the object's size, the vtable is there
- chrisseaton 5y agoIf your class never needs a runtime virtual call because all virtual calls were monomorphisable, why would it still include a virtual table? What’s the point?
- jcelerier 5y ago> If your class never needs a runtime virtual call because all virtual calls were monomorphisable, why would it still include a virtual table? What’s the point? if that was not the case, it would mean that the size of the type would vary, which would break a lot of expectations. Imagine the following: class obj { virtual int space_used() { return sizeof(obj); } }; class sub1 : obj { int space_used() override { return sizeof(sub1); } }; it would be very normal for someone to call `space_used();` at some point, and use that size at another point (with a different instance), for instance for allocation ; if that size was to change because that instance was "optimized" by the compiler, that could wreak havoc.
- chrisseaton 5y agoAh I assumed it knew about all classes before it started running compile-time code.
- jeffbee 5y agoIt doesn't have a vtable, it has enough space for vptr, but vptr isn't even initialized. It's true that thing is larger than your trivial class.