I will give the first person to correctly explain why this compiles a nice compliment
#include <iostream>
class A {};
int main() {
std::cout << &(A() = a) << "\n";
}
Oops there is missing declaration of the variable a
. This is what you should be examining:
class A {};
int main() {
A a;
std::cout << &(A() = a) << "\n";
}
@wallhackio this probably isn't the answer you're looking for but it's becuase c++ is a bastard unholy mess of a language
@holly this is correct but unfortunately will not be accepted as an answer
@wallhackio i'm fine with this i was in it for the quipping
@wallhackio Getting headpat by Clodsfire sounds like a great way to be set on fire...
Good thing that Vaporeons are resistant to fire damage
And also too stupid to understand why this code compiles despite a
being an undeclared identifier...
@wallhackio my explanation was just “C++ is bad” but i’m guessing you want something a bit more technical
@Lady I seek not only truth, but specificity and truth
@vaporeon_ evidently I have burnt my brain today
@vaporeon_ it should be #include
@vaporeon_ what the fuck is mastodon is hiding the hashtag
@vaporeon_ it should be #include <iostream>
@wallhackio Maybe it's stripping out <iostream>
because it thinks that it's a HTML tag that it doesn't recognize?
@vaporeon_ @wallhackio likely
@wallhackio OK, if the a
is declared, then I think that A()
is creating a new object of class A
, and then = a
copies the values from a
into it, and, like any assignment, returns its right part, so returns a
, and &
will simply return the address of a
.
But I'm too stupid for C++, I'm probably wrong
@vaporeon_ @wallhackio this is very close but there is some pedantry here
&
) out of the result of the expurression. (no i do not even know how to begin to explain what an lvalue refurence “is”)@aescling @wallhackio > the value of the left side (after assignment) is the value of the assignment expurression
Oh... So will &(A() = a)
, instead of returning the address of a
, return the address of the newly-created A()
?
@vaporeon_ @aescling yes
@vaporeon_ @aescling but the result of A()
is an rvalue, which is not allowed to be given to & or appear on the left side of an assignment. That is the mystifying part here
@wallhackio it's just outputting the address of a temporary object? and then a newline? why wouldn't it compile? there's not even any UB here i think (since you never dereference the resulting pointer)
@alyssa a constructor returns an rvalue which is not supposed to be allowed to appear on the left side of an assignment
@wallhackio at least in C++23 it returns a prvalue, which can be implicitly converted to an xvalue. although, hmm, an xvalue is only a glvalue, not an lvalue (which the assignment operator requires) so that doesn't seem to explain it. alas idk
I am increasing the bounty. Whoever can explain this gets 1) a compliment, 2) a headpat, and 3) a kissy on the forehead