Hacker News new | ask | show | jobs
by johnny531 5578 days ago
If only c++ had some sort of static type system which could be leveraged to provide compile-time checks...

But seriously, this is a large part of the power of c++'s type system. Taking the article's example, if the argument types were of (user class) 'non_zero_float', there's no possibility for error.

You still have to check that your input is non-zero at some point, but you've now focused it into one place (the 'non_zero_float' class ctor), and other chunks of your program depending on those type semantics no longer need to worry about it.

1 comments

You can really make that type do a compile-time check on runtime values?

It would be better to have some way of getting the compiler to optimize constraints, perhaps by proving at compile time that the error is impossible.