logoalt Hacker News

dbdryesterday at 7:43 AM1 replyview on HN

> Even in C++, with appropriate rules, restrictions and discipline you can write programs that are guaranteed to be at least as safe as any Rust program

If by discipline, you mean running something akin to the borrow checker in your head, that's essentially tautologically true. The issue with that is that it's mentally draining and/or you will still make mistakes sometimes.


Replies

adrian_byesterday at 12:05 PM

No, I mean by using only custom types for things like arrays, strings and pointers, which do access checks and automatic memory release, and not using unsafe features like the built-in arrays, strings and pointers, or the incorrect integer type conversions inherited from C.

For maximum safety, beyond what Rust offers by default, in C++ it is easy to replace the built-in integer types with custom integer types, which check for overflows and allow only the correct type conversions. It is also easy to define distinct types for various kinds of physical quantities, for increased safety.

You do not need to run anything in your head. With appropriate type definitions, a C++ compiler will do anything that is required.

The problem is that because of the requirement for backwards compatibility, C++ is a huge junk collection. I think that more than half of C++ consists of obsolete features, which should never be used in new programs, and this is a serious difficulty for newbies. There are various C++ style guides, but in my opinion even most of those are not very inspired.

Despite of its defects, C++ still has the advantage of extreme customizability. It is easy to write programs that appear to be written in a language that has no resemblance with C++ (inclusively by having different keywords and what appears to be a different syntax), but nonetheless they are valid C++ programs.

Such a customized C++ variant can mimic any safer language.

show 1 reply