Hacker News new | ask | show | jobs
by uecker 9 days ago
References hide important semantic information:

    int x = 1;
    foo(x);  
    // What values has x?
In C the answer is obvious, in C++ due to the existence of references and the poor way they are designed, it is not anymore.
2 comments

What's wrong with checking the signature of foo to see what it does?
Having to check in the first place.
I understand that doing a mouse over in the IDE is something too advanced. /s
There is a huge different between having to actively explore all variables to find out what is behind them to simple looking at the code.
Yeah, that is what happens when one eschews modern IDE tooling, more work for easy stuff.
No, sorry, this has nothing to do with tooling. I do not want to move my mouse over all variables and read the fine print before being able to reason about the code.
Except for any codebase of meaningful complexity you end up with functions that mix value and pointer variables, and whatever "obvious" signal you get out of the simple cases disappears into the noise. At the end of the day there is no replacement for understanding the signature and behavior of the function that is being called.

Considering that every other "C family" language community gets by just fine with call sites that look like using references, I think calling this element of references "poorly designed" is quite a stretch.

Specially coming from a language that after 50 years still doesn't have slices to fix their strings and arrays, something that Dennis Ritchie proposed when he still cared about the standard.
I use slices in C, I just have to define them myself.
Yeah, what a great solution, I can also use slices in Assembly, one just needs a proper macro processor like MASM and TASM from the 1990s.
yes, and if you assembly this is fine solution. Also there is no fundamental reason C++'s library types using templates are better to macros. The mechanism is basically the same.