70 points · by chmaynard · 5y ago · 81 comments
But they are permitted to do exactly this. This is one of the unusual things about copy-elision in C++. The standard says that under certain circumstances, the compiler is permitted (but not required) to elide a copy operation even if this causes a change in observable program behaviour.
* https://en.wikipedia.org/wiki/Copy_elision
* https://en.cppreference.com/w/cpp/language/copy_elision (the article referenced by the post)
All this is an argument for "rule of zero": write your classes in a way that the default copy and move constructors and destructor do the right thing. That gives the compiler latitude to do the right thing in places that it otherwise could not.
Some code obliged to do extra work will be able to do less of it when compiled with a compiler that knows C++23, provided you have followed rule of zero.
I understand why the committee was unable to get destructive moves into the standard, but I do think that is one thing Rust got right that I really wish C++ had.
People say A few hundred dollars is nothing to a professional software engineer, but few C++ developers actually buy it, so the legalese they consult is often not the letter of the law but a proxy like cplusplus.com, replete with the occasional error.
Stay on the happy path of not having to be an ISO expert.
An `std::unique_ptr<>` member is not just safer, it is also faster if it enables the compiler to generate its own various constructors and destructors.
Right, but if you're sprinkling your code with temp variables, specially with a language such as C++ where copy elision is far from ensured, then you're already not averse to the idea of instantiating variables left and right.
One of the complaints about c++ is how many obscure features it has. Most of those obscure features are for authors of the standard library implementations (or other libraries intended for widespread use), not for user code. If you look at the standard library source it’s very hard to read because it’s full of things to transparently handle weird corner cases in order to avoid user surprise.
But in my code I don’t use any of that stuff. There’s a pretty straightforward modern language in the middle of that, and not even a huge one.
Seems like that particular Abseil ToTW isn’t public, but it alluded to this practice being banned in the C++ STL.
Or for another example, writing code that can be compiled by different versions of a single compiler, clang, e.g. across the Android NDK, XCode, latest LLVM on Linux, and Emscripten, will occasionally give surprising results. To say nothing of supporting msvc or gcc.
> There’s a pretty straightforward modern language in the middle of that, and not even a huge one.
made me literally mutter "woah... really?" I've always had the impression, and granted it is based primarily in ignorance and group think (looks around), that learning C++ is an endeavor that takes years because the language is so complex. And so, I would love any resources that make it more tractable... or does it really boil down to don't use "obscure" features you don't need?
Contracting is the main point that makes me happy not having to deal daily with C++.
Nowadays I just use it for native library wrappers, as it is better than plain C, but still hoping for FFI improvements on the languages I use that will reduce that need even further.
When you consider that references in C++ need to be explicit you have the following solution:
I don’t understand what caused the author to assume that a compiler would elide the copy when passing a value argument to a function. That seems like an unfounded assumption to me and I don’t think there is anything sad about it not being true.
To avoid the copy in all cases the “consume” function should probably accept a reference argument. That would be idiomatic C++.
Widget && temp = complicated_long_function(...);
consume(temp);
Am i missing something?The pointer deref is unavoidable if you pass a large enough object by value, which is what’s happening here. On x86_64, generally values larger than 16 bytes are passed by reference transparently.
Edit: I agree with the other commenter about taking in a Widget by reference instead of by value in the consume function.
I’d prefer code like this use the move operation to make the code reasonably efficient and maintainable. Then, only after its proven the line is on a critical performance path, it can be further optimized later.
This blog post was just getting good, then it stopped!
Unless you are an experienced library writer you should think very, very hard about writing copy or move constructors (and even then, think again).
Yeah, so USE std::string in your classes, then no copy constructors written by you will be needed. If you find yourself heap-allocating things explicitly with "new", then you are almost certainly doing something wrong.
Why does it matter who wrote the copy constructor? In the author's example, they did not write a copy constructor, they USEd std::string.