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 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

  • strictly, the behavior of (non-overridden) assignments (yes you can override assignment in this language) is that the value of the left side (after assignment) is the value of the assignment expurression. this matters because of potential type coercion shit
  • the default behavior of an assignment is guaranteed to return an lvalue, this is why you can create an lvalue refurence (which is what &) 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 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

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.