Bug 9019 – invariant does not compile/run if class does not define constructor

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-11-13T16:34:00Z
Last change time
2012-11-15T00:23:58Z
Keywords
wrong-code
Assigned to
nobody
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2012-11-13T16:34:53Z
class C { invariant() { assert(0); } } void main() { C c = new C(); // should fail after ctor invoked } C has an implicitly generated constructor, but the invariant isn't compiled or it doesn't run. If you add an explicit empty constructor then it does get added.
Comment #1 by maxim — 2012-11-14T04:40:52Z
(In reply to comment #0) > C has an implicitly generated constructor Why? It seems there is no constructor in case like this. > but the invariant isn't compiled or > it doesn't run. If you add an explicit empty constructor then it does get > added. It is compiled but is not run because there is no ctor. The spec says that invariant is called when ctor completes but it does not say what happens when there is no ctor. You actually asking either create dummy ctor for cases like this or call invariant just after allocating class object which is not efficient - the first call to public function will abort the application anyway.
Comment #2 by clugdbug — 2012-11-15T00:20:52Z
Duplicate of one of the oldest open bugs! *** This issue has been marked as a duplicate of issue 519 ***
Comment #3 by andrej.mitrovich — 2012-11-15T00:23:58Z
(In reply to comment #2) > Duplicate of one of the oldest open bugs! Good thing it's assigned to someone, that means it will be fixed soon! Probably before new years eve 2006. :P