72 karma · joined Nov 23, 2022 (4y ago) · 62 submissions
comment 4y ago · link · parent
C++ has serious problems. Linus Torvalds agrees. Bryan Cantrill agrees. Many, many experienced systems programmers agree. Rust solves many of the problems with C++, as a systems programming language. That is the scope of my argument.
I am not really sure in the modern software development world what problem dynamic linking of stdlib solves, especially since so much of stdlib would be monomorphized and/or inlined. C++ with any level of templates is far more unfriendly to dynamic linking -- and again, that is the only language of comparison in scope for this article.
comment 4y ago · link · parent
My "example" is showing an equivalent to what the C++ compiler actually outputs, inlining the `std::string` abstraction, given the input code, which in this context was C++03, and before emplace.
But emplace doesn't save you here! Emplace doesn't save on heap allocations over a `strdup`, if you're emplacing something from a string literal like this.
Doing:
``` vec.emplace_back("here's a string"); ```
Does indeed do an allocation for the string. It has to, because every `std::string` must manage a heap allocation (modulo the small string optimization which is a nitpick). The string literal is in the `text` section of the binary and can't be the backing for the `std::string` object, which must be in read-write memory and on the heap.
So the `strdup` is correct as an equivalent to what the C++ compiler would output with `std::string` inlined for the example code it covered.
Also, the freeing of the empty object with move semantics is NOT always optimized out, not even close. It can't be, not when those calls can cross a library boundary or be arbitrarily wonky.
You write as if C++ has one optimizer. Rust shares an optimizer with one of the most popular C++ compilers. Implying Rust is somehow "behind" C++ on optimizations is just ill-informed.