News for hackers. Stuff that matters.
Re:Const all the things? by GuB-42 on Monday January 24, 2022 @06:09AM (#30056091)
I absolutely hate non-const reference parameters. Personally, I never use them except for operator overloading or stl functions.

The reason is, as lelanthran said, that when I see func(param), I don't expect param to be modified.

The time I lost because of non-const reference parameters adds up to days, maybe weeks. In fact, bugs caused by stealthy references are often way harder to find than null pointer crashes. Null pointer crashes are among the bugs that concern me the least, sure crashes are bad, but the core dump often tells you exactly what's wrong, and they rarely lead to non-DoS exploits. See: offensive programming.

Now that I think of it, maybe IDEs should highlight non-const references in function calls.

I think it is unfortunate because I like the extra guarantees that references offer over pointers but the lack of explicit (de)referencing is enough for me to avoid.

Re:Const all the things? by jcelerier on Monday January 24, 2022 @06:33AM (#30056276)
> The time I lost because of non-const reference parameters adds up to days, maybe weeks.

Well, we have wildly different experiences, I can't remember a single instance where this was an issue in the projects I've been involved with.

Re:Const all the things? by GuB-42 on Monday January 24, 2022 @08:49AM (#30057512)
And I believe you, here are the possible explanations:

- Personal experience: it is not a construct I use, and most of the code I work with don't use it either, so when it happens, it is a surprise. People who come from a pointerless language like Java may expect it.

- I work with a lot of messy code: large code bases I barely know, written by many people from different companies, without a style guide. So one function may use references and the next one may use pointers, adding to the confusion.

- Of course, documentation is misleading (to the point that I make a conscious effort not to read it), and so are function names. Things like getChild(n) that increments n when pretty much every getChild(n) function everywhere passes n by value. Terrible code, that's why there is a bug in the first place, bugs are usually not in the best written parts.

- I actually like jumping in other people terrible code and fix bugs, it is an interesting challenge and I became rather good at it where I work. As a result, I am often given these kinds of job. A few days is not that much compared to my entire experience of debugging terrible code, but enough to be significant.

- Of course I spend a lot more time fixing memory corruption (and the problem with stealthy reference is that it can look like memory corruption). But in my experience, references over pointers don't help that much with it. It helps against null pointer dereferencing, but as I said before, these are usually easy bugs to fix.

Re:Const all the things? by nemetroid on Monday January 24, 2022 @09:25AM (#30057919)
> Now that I think of it, maybe IDEs should highlight non-const references in function calls.

CLion highlights this with an off-color "&:" in front of the parameter.

Re:Const all the things? by moonchrome on Monday January 24, 2022 @07:47AM (#30056960)
C# requires explicit "out" on call site, I wonder if you could make a macro and lint rules for C++ to require this :) Haven't touched C++ in almost 10 years so not up-to-date on how good/extensible linters are.
Re:Const all the things? by gpderetta on Monday January 24, 2022 @09:26AM (#30057932)
You can create a trivial wrapper:

   https://godbolt.org/z/c5YeP6feG
The usual issue with these sort of things is that they are not idiomatic (but then again, there is no single idiomatic C++).

edit: std::reference_wrapper is of course the standard blessed way to do this, but still not idiomatic.

Re:Const all the things? by chrisshroba on Monday January 24, 2022 @07:40AM (#30056904)
> Now that I think of it, maybe IDEs should highlight non-const references in function calls.

This is a great idea. When I type `ls` in a shell, I get important information from the colors of the output: one color for normal files, another for executables, another for directories, etc.

This would work well for parameter types, even if it were as simple as "white means it cannot be modified (pass by value, const reference, etc.) and yellow means it can (non-const reference, pointer, etc.)"

I wonder if this exists?

Re:Const all the things? by andrepd on Monday January 24, 2022 @09:10AM (#30057723)
I believe it's called "semantic highlighting".
Re:Const all the things? by cyber_kinetist on Monday January 24, 2022 @12:19PM (#30060701)
It would be really nice for C++ to add a mandatory “out” keyword for these parameters, just like C#. In that way you are forced to specify that this is an output parameter both at the caller and the callee, which makes things much more explicit and readable.
Re:Const all the things? by LeSaucy on Monday January 24, 2022 @12:29PM (#30060864)
This is a great talk by Herb Sutter that goes into this in detail https://www.youtube.com/watch?v=6lurOCdaj0Y