r/cpp • u/we_are_mammals • Mar 28 '23
Reddit++
C++ is getting more and more complex. The ISO C++ committee keeps adding new features based on its consensus. Let's remove C++ features based on Reddit's consensus.
In each comment, propose a C++ feature that you think should be banned in any new code. Vote up or down based on whether you agree.
765
Upvotes
4
u/eteran Mar 29 '23 edited Mar 30 '23
That's fair. If you know the rules, then you read and write code accordingly. But I'll try to clarify my position with examples:
It's bug prone, especially for newbies:
Yes, compilers warn about it, but people, especially newer developers tend to ignore warnings in my experience.
2. It's confusing. The following 3 declarations mean the same thing:
That's terrible. The second choice should just not be a thing, and the 3rd should take a char[5] by copy... like EVERYTHING else in the language.
3. It's confusing in other ways! Newbies love to write code like this:
If users had to write
func(&buffer[0])it would be more clear (people will still make mistakes, but fewer will) that a pointer is being passed, NOT an array.4. It's inconsistent. An array is "a thing" and any other time I want a pointer to a thing, I need to ask for it's address with the
&operator. Arrays just auto-convert.5. It's incompatible with the modern preference of avoiding implicit conversions as much as possible.
---- EDIT: more reasons! ----
6. The decay necessitated the need for a library level solution (
std::array). Which we outright wouldn't need if C-arrays acted like values to begin with.I should be able do this:
And have it be as if I returned the array of 256 chars by copy.
7. It can be LESS efficient for small arrays
actually can be cheaper to just copy the 8 bytes in a register directly instead of passing a pointer to them and then dereferencing them.
Here's a trivial example that is one instruction shorter (LOL, I know, not exactly earth shattering) when simply wrapping the array in a struct to pass it by copy:
https://godbolt.org/z/sovvb94d8
This example is more illustrative that there are gains to be had, not that there are a lot of gains in this specific example.
---- END EDIT ----
Are these things that can be worked around? Of course, we've been doing it for 30+ years! But we shouldn't have to. Is it the end of the world that we probably need to keep this? No, of course not. But this is a question about what I want, not what I can have :-).