|
|
|
|
|
by scott_s
4428 days ago
|
|
The problem that Rust solves is that your advice, while good, is still advice. I absolutely agree that naked pointers are a code smell, and stack allocated objects should be the norm, with passing around (const) references to them. And RAII wrappers are great. But all of that are patterns of use, enforced mostly by convention. In Rust, that's enforced by the language itself, and violating it will be a compiler error. The following kind of shenanigans won't be allowed outside of unsafe regions: int main()
{
int on_stack;
int& ref = on_stack;
int* ptr = static_cast<int*>(&ref);
delete ptr;
return 0;
}
Yes, it's obviously bad code, but C++ happily let me write it, and it compiled with no warnings under -Wall -Wpedantic. |
|