Bug 3655 – Virtual functions without bodies are not optimized away.
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2009-12-27T04:12:00Z
Last change time
2015-06-09T01:26:55Z
Assigned to
nobody
Creator
e.insafutdinov
Comments
Comment #0 by e.insafutdinov — 2009-12-27T04:12:27Z
This is compiled fine, as final method without a body is optimized away:
class Boo
{
final void foo();
}
However this does not:
class Boo
{
void foo();
}
linker fails with:
main.o:(.rodata+0x194): undefined reference to `_D4main3Boo3fooMFZv'
This might be because function is put into vtable. Is it easy to fix?
Comment #1 by e.insafutdinov — 2009-12-27T04:13:25Z
(In reply to comment #0)
> This is compiled fine, as final method without a body is optimized away:
>
> class Boo
> {
> final void foo();
> }
>
Of course it is optimized away if it is not used.
Comment #2 by e.insafutdinov — 2009-12-27T05:03:55Z
Ok, actually I am wrong here, final methods are not optimized away - they just don't exist. And if nobody uses them - you are safe. But the similar behavior should be present for virtual functions as well I believe - virtual function without a body should not be present in vtable.
Comment #3 by bugzilla — 2009-12-28T21:14:32Z
The compiler is working as designed.
The final function is not virtual, so it is never needed in the vtbl[]. Hence, there is no undefined reference to it in the vtbl[].
The non-final function is virtual, and so a reference to it is put into the vtbl[]. The function's implementation must exist somewhere, and if it did, the linker would put a reference to the implementation in the vtbl[]. This feature allows one to have an implementation that is hidden from the user of the class.
If you truly do not want to implement the function, declare it as 'abstract'. Then, a NULL is put in the corresponding place in the vtbl[].
Not a bug.