I disagree that C++ isn’t necessarily type-safe, or that C++ can’t be written in a type-safe manner. If you stick to C++ and don’t use c-style casts, reinterpret_cast
, and raw pointers, you have a degree of type-safety. It’s not absolute, of course; if you want that, you should probably go write your project in Ada. (I bring Ada up here because Ada is a phenomenal language, and though it has it’s issues and warts, as does any programming language, it’s probably one of the safest, if not the safest, languages out there, and I don’t think even Rust has managed to get close, but it does try.) No, the problem is that (modern) C++ still allows unsafe methods. I suspect that this is for performance, though it’s hard to buy that excuse when I think the checking if a std::unique_ptr
is nullptr
or a std::optional
is valueless conditional would take only a few cycles, assuming the compiler doesn’t figure out it’s value beforehand and either optimizes it away entirely or gets rid of the conditional check and leaves only the “throw this exception” or “return it’s value” statements in it’s place. The (proper) way to work with std::optional
s and std::*_ptr
s is to be smart, too, because a “smart” pointer is not going to protect you from your own bouts of idiocy. (I for example will use std::unique_ptr
/boost.unique_ptr
wherever I can, and rarely ever do I use std::shared_ptr
, even in concurrent or parallel code.)
No, I think the real problem underlying insecure code is not the language. C/C++ could be the most type-unsafe language in all of existence and I still would state this as fact, because the real problem is the fact that those that teach programming (colleges, universities, schools, …) don’t emphasize secure coding practices at all. At least, a lot of them don’t. Mine for example didn’t; oh, there was plenty of emphasis on writing code that actually ran in parallel, but there wasn’t a peep about secure coding practices or writing secure software. This doesn’t just happen in the education sector, but in tutorials and books too. So people learn how to code, presumably in C++, but what they don’t learn is about the unique/shared pointers, optionals, etc., instead learning about C functions, where type-unsafety is guaranteed. (One of my professors, I quickly found out, was well-known for writing half-C/C++ code, and teaching his students, myself included, that we too should do this, because it was what he knew, and I don’t know if he even knew about the advancements of C++11/14/17/20.) So, here we are, with students learning that writing insecure code is fine, and then they go into the workforce and… Wouldn’t you know it: this same attitude is promulgated and encouraged, which further makes the entire concept of writing secure code less and less known, unless your in a sector where it’s legally required (because if you don’t, the government is going to fine you into non-existence and most likely jail your C-suite executives, something I think should happen far more often than it does, but that’s another topic for another day). My point is, people go on and on and on about writing memory-safe code, or safe code in general, but this isn’t taught from the beginning when people learn how to code. It’s only taught when it’s absolutely required, and by that point, your so used to writing code in an unsafe manner that it’s difficult to change that mindset.