int& a = *(int *) nullptr; // doesn't crash
int b = a; // crashes
References were a mistake
Good movie but you got filtered
C++ has some weird syntactic sugar, but that doesn't mean references are a bad concept. Rust does them much better.
I didn't, I completely understand what's going on here, I've actually exploited it before in contexts where I wanted the nicer syntax of references but still wanted nullability.
I'm just complaining about it anyway because it's funny
Oh understandable then
Is this thread about C++ specifically or references in general?
>I wanted the nicer syntax of references but still wanted nullability.
I strongly disagree with everything you stand for
so references are basically just macros then
Use a white man's language.
References are just pointers that can't be used like pointers and implicitly decay to the value they point to
but int& a = *(int *) nullptr; doesn't actually dereference the nullptr, it just does mov QWORD PTR [rbp-0x8],0x0
Yes, that's what I'm saying.
int& a isn't the value that *(int *) nullptr points to, it's a pointer that can't be used like a pointer and implicitly decays to the value it points to.
how does it decay to the value if the value is never dereferenced
is that battler
>how does it decay to the value if you never write an expression where that decay would happen
>how does an array decay to a pointer if you never pass it anywhere
>how is the sky blue if i don't look at it
but you're not saying int& a = nullptr you're saying int& a = *(int *) nullptr; which is entirely different
looks like a problem with pointers
Well, yes, the first one won't compile, because there's no implicit conversion from plain old pointers to pointers that can't be used like pointers and implicitly decay to the values they point to. The second one does compile, because that's the correct explicit conversion.
so a reference is just a macro that ignores dereferencing and sets its value to the address that you were trying to dereference?
Other way around. In theory, dereferencing always produces a reference, never a value. But references implicitly decay to the values they point to. So if you dereference a pointer and try to *act* like you're getting a value from it, it will always work, because of that implicit decay.
but in this instance, int& a = *(int *) nullptr; the compiler isn't actually dereferencing anything, it just stores the memory address (which is nullptr) that you wanted to dereference, right?
>the compiler isn't actually dereferencing anything, it just stores the memory address
Yes, that's what dereferencing is.
Dereferencing converts a pointer to a reference.
It does not follow a pointer to the value.
That's the job of the reference decay.
>Dereferencing converts a pointer to a reference
*(int *) nullptr converts a pointer to a reference? i thought it returns a l-value?
Yes, an lvalue is a kind of reference
ok, so if that is the case, then both cases should crash, but they don't. why?