struct X
{
this(int a)
{
if(a == 0)
this(a, 1);
else
throw new Exception("");
}
this(int a, int b) {}
}
The code above fails to compile with "one path skips constructor" error. It can be worked around by adding "assert(0);" to the else block, but shoudn't throwing exception be enough?
Comment #1 by dkorpel — 2019-11-15T14:00:09Z
An exception represents a recoverable error that can be caught and then you end up with a partially constructed object.
An assert(0) represents an unrecoverable error, the state of the program afterwards is undefined so partially constructed objects are possible.
To me the error seems appropriate.
Comment #2 by robert.schadek — 2024-12-13T19:06:13Z