You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The example is incorrect anyway. Whatever the object X[ {1,2,3} ] of type Z is, it will never be updated with the object Z(7), as the object X[ {1,2,3} ] is destroyed at the end of the expression X[ {1,2,3} ] = 7; , if the return of X::operator[](std::initializer_list<int>) is not an lvalue reference. In other words, even for a class object,, a subscript operator has to return an lvalue reference, if it's going to be used to update an object of its own class.
Z could be a proxy object that refers back to the X that created it. It could even be X&. It's not relevant to the example, and the example doesn't need to be a complete, working program doing something useful.
To expect someone to guess that Z could be a proxy object referring back to X in the alluded example, contributes to the aura that the Standard already has nowadays, i.e., of an unintelligible document, to the point that sometimes it seems to have mystifying purposes.
Nobody is expected to guess that, because it's not relevant to the example. You have twice stated the example is invalid code and I've provided evidence to the contrary in the form of suggestions for the type not shown in the example.
It doesn't matter exactly what Z is, or whether the example could usefully update x. That's not the point of the example.
The point is you don't need to know what it is, because that's not what the example is about. The overloaded operator returns something that can be on the left hand side of an assignment. There are types that make that valid, so stop assuming it's a type that makes it invalid.
Activity
jwakely commentedon Apr 19, 2018
That's not true if
Z
is a class type.jabelloc commentedon Apr 20, 2018
The example is incorrect anyway. Whatever the object
X[ {1,2,3} ]
of type Z is, it will never be updated with the objectZ(7)
, as the objectX[ {1,2,3} ]
is destroyed at the end of the expressionX[ {1,2,3} ] = 7;
, if the return ofX::operator[](std::initializer_list<int>)
is not an lvalue reference. In other words, even for a class object,, a subscript operator has to return an lvalue reference, if it's going to be used to update an object of its own class.jwakely commentedon Apr 20, 2018
Z
could be a proxy object that refers back to theX
that created it. It could even beX&
. It's not relevant to the example, and the example doesn't need to be a complete, working program doing something useful.jabelloc commentedon Apr 20, 2018
To expect someone to guess that
Z
could be a proxy object referring back toX
in the alluded example, contributes to the aura that the Standard already has nowadays, i.e., of an unintelligible document, to the point that sometimes it seems to have mystifying purposes.jwakely commentedon Apr 20, 2018
Nobody is expected to guess that, because it's not relevant to the example. You have twice stated the example is invalid code and I've provided evidence to the contrary in the form of suggestions for the type not shown in the example.
It doesn't matter exactly what Z is, or whether the example could usefully update x. That's not the point of the example.
jwakely commentedon Apr 20, 2018
Z could also be typedef for
int&
The point is you don't need to know what it is, because that's not what the example is about. The overloaded operator returns something that can be on the left hand side of an assignment. There are types that make that valid, so stop assuming it's a type that makes it invalid.