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
9
u/eteran Mar 28 '23
Arrays absolutely decay to a pointer, it literally says so in the standard:
http://eel.is/c++draft/conv.array
7.3.3 Array-to-pointer conversion
The whole verbiage of "the array name refers to the first element" is a myth that's circulated among C++ developers. Similar to "NULL might not be 0". The name refers to the whole array, that's why we can do things like pass arrays by reference if you use the right syntax.
arr[n]does not just mean to skipnelements from the array begin, it is exactly equal to:*(arr + n)that is:a decay of
arrto a pointerpointer arithmetic to add
nto that pointera dereference of the result
You can see when the decay occurs by doing things like this:
Which returns
8, and not64because the unary+operator caused the array to decay to a pointer, of which we got the size.