Pointer or reference: no difference?
There was some discussion recently on Twitter or something, with people claiming that there was no difference between a pointer and a reference for the compiler. Well it’s “mostly true” but in some critical cases it’s very wrong.
In fact, it is so wrong that in the past I was forced to remove all references from some classes in our codebase, because they broke our binary serialization system.
I’m not going to spoil it for you, it’s more fun if you just run the following test first with a pointer then with a reference, and see what happens.
Surprised, like I was? You should not be, it is a perfectly normal, expected and documented behavior. The compiler does exactly what it should be doing.
But it shows that pointers and references are not always “the same” w.r.t. generated code, nope.
March 11th, 2016 at 6:12 am
I think what you can qualify it as is \pointers and references result in the same code on a majority of all compilers, for all usage that is valid for both pointers and references\. Taking the address of a reference is not valid C++ (as per $8.3.2/3, it is undefined if a reference takes any memory or not at all), and once you do something undefined the compiler is free to do whatever it wants. As a humorous version of that, an old version of gcc would launch the games \rouge\, \nethack\ or \tower of hanoi\ whenever it encountered code with undefined behaviour (and that is conforming to the standard)
March 11th, 2016 at 12:56 pm
For those, who is interested in details, here’s a few points from the spec:
8.3.2.4 It is unspecified whether or not a reference requires storage.
8.3.2.5 There shall be no references to references, no arrays of references, and no pointers to references.
March 11th, 2016 at 10:48 pm
The real question is: why are you still using RAR?