News for hackers. Stuff that matters.
Re:The Sad Truth About C++ Copy Elision by MaxBarraclough on Saturday April 03, 2021 @11:01AM (#26681572)
> If compilers were to unpredictably remove copies, and thus remove pairs of copy/move constructor & destructor calls, they might break your code.

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)

Re:The Sad Truth About C++ Copy Elision by ncmncm on Saturday April 03, 2021 @11:30AM (#26681819)
They are permitted in certain cases, not in others. It is proposed to widen the former list for C++23.

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.

Re:The Sad Truth About C++ Copy Elision by partiallattice on Saturday April 03, 2021 @04:11PM (#26684328)
One tough thing about rule of zero classes is that many to most end up having an implicit 'the object has not been moved from' precondition on methods. The classic example is a class with a unique_ptr member that is never null except after move, so none of the methods have null checks.

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.

Re:The Sad Truth About C++ Copy Elision by tragomaskhalos on Saturday April 03, 2021 @06:04PM (#26685151)
People bang on about borrow checking etc but really it's move semantics that's the million dollar idea in Rust - it just obliterates a huge Gordian knot of complexity in one stroke
Re:The Sad Truth About C++ Copy Elision by ncmncm on Sunday April 04, 2021 @04:54AM (#26687903)
In Rust you cannot write a move constructor. That proves limiting. But Rust move construction, also, cannot fail, which is liberating.

Rust cannot abandon backward compatibility with itself, so will increasingly be stuck with old choices, like C++, and will soon be fully as complex and infuriating. But it will, like C++, remain usually more useful than alternatives.

Re:The Sad Truth About C++ Copy Elision by MaxBarraclough on Saturday April 03, 2021 @05:24PM (#26684873)
Agree. Can't really blame C++ for it being difficult to retrofit something like that. It's akin to the lack of const in Java: they'd add it if it were practical to do so.
Re:The Sad Truth About C++ Copy Elision by pjmlp on Saturday April 03, 2021 @03:23PM (#26683922)
This is the kind of nuances that make it increasingly hard to look at C++ code and be sure of its semantics, without diving into compiler flags and standardese.
Re:The Sad Truth About C++ Copy Elision by MaxBarraclough on Saturday April 03, 2021 @04:17PM (#26684368)
Not to mention the paywalled C++ standards documents.

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.

Re:The Sad Truth About C++ Copy Elision by pjmlp on Saturday April 03, 2021 @05:36PM (#26684951)
There are the drafts, but even if final standards were available for free, this kind of knowledge is too fine grained to keep all in head across language revisions.

I bet many ISO committee members aren't able to answer "what are the semantics of this code" with looking into the standard.

And there lies the problem, it is not possible as contractor jumping into a foreign code base to be certain of everything that is going on there.

Now imagine the task of upgrading a codebase to a newer compiler and language version, while assuring everything keeps working the same way.

Re:The Sad Truth About C++ Copy Elision by ncmncm on Saturday April 03, 2021 @07:47PM (#26685765)
No one is obliged to pay "a few hundred dollars" to see the text of ISO 14882. The Final Draft International Standard online is guaranteed to differ from the formal Standard only on the title page.

But cppreference.com is generally easier to understand.

Re:The Sad Truth About C++ Copy Elision by MaxBarraclough on Saturday April 03, 2021 @09:09PM (#26686134)
> "a few hundred dollars"

Care to explain the scare quotes? ISO charge 198CHF, [0] which is $210 USD. ANSI charge $250. [1]

> The Final Draft International Standard online is guaranteed to differ from the formal Standard only on the title page.

Do you have a source for that? From what I can tell the differences are expected to be minor, but that's as far as the guarantee goes.

If what you say is true, why would anyone ever both paying for the completed document, and why would ISO expect to derive profit from paywalling it?

> cppreference.com is generally easier to understand

Sure, I'm not saying it's a worthless website, far from it, but errors can and do creep in.

[0] https://www.iso.org/standard/79358.html

[1] https://webstore.ansi.org/Standards/ISO/ISOIEC148822020

Re:The Sad Truth About C++ Copy Elision by ncmncm on Sunday April 04, 2021 @04:45AM (#26687863)
> Care to explain the scare quotes?

Because nobody not contractually obligated needs to pay it. Some entities allow themselves to be so obligated, but you are not obliged to mimic them.

> ...why would ISO expect to derive profit from paywalling it?

ISO is an old organization. For paying it, as with the "Dine and Dance" sign on the hotdog stand, you can if you want to.

> From what I can tell the differences are expected to be minor, but that's as far as the guarantee goes.

That is as far as ISO goes. But SC22/WG21 goes the rest of the way. ISO has no authority to change the text from the FDIS in any way that affects the agreed-upon meaning of the text. In practice this means no changes at all. SC22/WG21 is not obliged to publish its FDIS, but is allowed to. It does; not all WGs do.

Cplusplus.com is, unfortunately, garbage. All we can do about that is say so.

While cppreference.com is not guaranteed to be precisely accurate, you are overwhelmingly less likely to misunderstand it than the Standard, so you come out well ahead. Anywhere it and the Standard differ, your compiler is likely to differ, too.

Re:The Sad Truth About C++ Copy Elision by MaxBarraclough on Sunday April 04, 2021 @07:56AM (#26688745)
> nobody not contractually obligated needs to pay it

Oh, right. You could just as well say academia's open access movement makes no sense as no one is pointing a gun at your head making you pay for access to academic publications. That would of course entirely fail to address the motivations behind the open access movement.

> For paying it, as with the "Dine and Dance" sign on the hotdog stand, you can if you want to.

Granted, the standards body gives you the easy option of not accessing the standard. That doesn't strike me as a compelling response. It would be challenging to come up with a model so closed and hostile that this wouldn't apply. (I suppose the Google v. Oracle case will decide whether it's even possible.)

It's perverse that ISO make it their business to obstruct and disincentivise access to standards. Sutter even has the gall to insist that it deserves to be called an open standard, [0] despite that ISO ensure that sharing this open standard is forbidden under copyright law, such that doing so may constitute a criminal offence.

They only paywall the final document because of the perception that there is value in having access to finished document rather than the final draft. You're essentially making the case that everyone who pays for the finished document is a fool, but evidently the perception is that the small differences are worth paying for at least some of the time, or else the paywall on the finished document would not be expected to derive profit. It's the same deal with preprints in academic publishing. But I'm just re-stating my earlier point.

> SC22/WG21 goes the rest of the way. ISO has no authority to change the text from the FDIS in any way that affects the agreed-upon meaning of the text. In practice this means no changes at all. SC22/WG21 is not obliged to publish its FDIS, but is allowed to. It does; not all WGs do.

Thanks, I appreciate the detail.

> Cplusplus.com is, unfortunately, garbage. All we can do about that is say so.

Really that bad? I find it useful as a general reference, but I have encountered subtle errors.

> cppreference.com is not guaranteed to be precisely accurate, you are overwhelmingly less likely to misunderstand it than the Standard, so you come out well ahead

That's an interesting point. 'Language legalese' can be pretty impenetrable, and that's not an issue exclusive to C++. You need to be a fairly advanced student of ANS Forth to make sense of this, [1] for instance. A standards document is not a tutorial, after all.

[0] https://herbsutter.com/2010/03/03/where-can-you-get-the-iso-...

[1] https://forth-standard.org/standard/core/DOES

Re:The Sad Truth About C++ Copy Elision by ncmncm on Sunday April 04, 2021 @02:01PM (#26691513)
> as well say academia's open access movement makes no sense as no one is pointing a gun at your head

The difference is that there is in fact no practical reason ever to buy a copy of ISO 14882. It takes contrived legal circumstances, such as a contract in which you are obliged to deliver someone the specific document published by ISO. In practice I have never encountered a real case, so this is a case of straining at gnats.

Recently, ISO relied on sales of Standards to fund its operations. Furthermore, allowing sale of inauthentic copies could endanger the public. But nobody benefits from offering a free download of the wrong thing, so perverse incentives have dissipated.

Anyone who chooses to spend the money can have the ISO document with its authoritative Title Page, and all of the satisfaction that Title Page brings them. They are fools only if they chose that over something else of actual value. Not everybody has a limited budget, or is spending their own money.

Cplusplus.com has a pattern of failing to fix errors it has been notified of, and of failing to keep up with current Standards. Use it if you like being misled. We might like to be able to exercise some authority to take down cplusplus.com, or force them to correct their falsehoods, but there is no such authority. The only real incentive they have to correct errors is to maximize ad revenue, but the marginal value of correctness is evidently limited.

The maintainers of cppreference are users of the Standard, and are inconvenienced by any misperceptions about what the Standard requires, so invest substantial effort to keep it in line, so far as tracking Defect Reports and resolutions.