News for hackers. Stuff that matters.
Re:Constructors and evil initializers in C++ by dabitude on Wednesday November 24, 2021 @03:37PM (#29334841)
I work without exceptions, and the typical solution is to crash the whole program (using assert) if the parameter is invalid. This way you can preserve the invariant.

To avoid crashing, the caller has to validate the parameters before calling the constructor, and handle the invalid parameters at this time rather than in the exception handler, but in the end it's the same.

Re:Constructors and evil initializers in C++ by jmmv on Wednesday November 24, 2021 @04:03PM (#29335109)
Crashing is fine if you are dealing with trusted input. But in the post, parameter validation was just an example. I've seen plenty of code using init methods to create network connections and the like -- and in those cases, you need proper error propagation.
Re:Constructors and evil initializers in C++ by spacechild1 on Wednesday November 24, 2021 @07:59PM (#29337111)
Another solution if you can't use exceptions is to have something like a valid() method that the user can call to verify that the object has been correctly initialized. Unlike an init() method, calling valid() is entire optional. If the caller is sure that the constructor arguments have been correct, they don't need to call valid(). Note that you can still use assertions inside the class.

Anyways, for small classes like this one literally anything is better than implicitly forcing the user to allocate on the heap. The author should have pointed out the massive drawbacks of this approach.

Re:Constructors and evil initializers in C++ by maccard on Friday November 26, 2021 @08:35PM (#29356300)
That mdthod is what the author is explicitly trying to avoid - having invalid state representabld. Using optional I'd a much better solution
Re:Constructors and evil initializers in C++ by spacechild1 on Friday November 26, 2021 @09:53PM (#29356856)
In the end there's not much difference between the two. In both cases you essentially call a function/operator to verify the correctness of the object. std::optional doesn't really enforce a check, it only makes it more obvious to the user. On the other hand, returning std::optional means that it will always check the invariant, whether you need it or not, so there is a potential performance penalty. Just something to be aware of.
Re:Constructors and evil initializers in C++ by omegalulw on Wednesday November 24, 2021 @09:22PM (#29337608)
> I work without exceptions, and the typical solution is to crash the whole program (using assert) if the parameter is invalid.

That's lazy. Why not return via error?

Re:Constructors and evil initializers in C++ by cyber_kinetist on Wednesday November 24, 2021 @11:36PM (#29338259)
It really depends on the thing you’re making, but for gamedev it is often regarded as good practice to assert on error and crash the whole game, so you can explicitly find those errors right away instead of waiting for it to silently cause more subtle problems down the line.