Skip to content

The example in [over.sub]p1 is incorrect #2042

Closed
@jabelloc

Description

@jabelloc

[over.sub]p1:

X::operator[](std::initializer_list<int>) returns a prvalue. It can't be on the LHS of an assignment as in

x[{1,2,3}] = 7;

Activity

jwakely

jwakely commented on Apr 19, 2018

@jwakely
Member

That's not true if Z is a class type.

jabelloc

jabelloc commented on Apr 20, 2018

@jabelloc
Author

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.

jwakely

jwakely commented on Apr 20, 2018

@jwakely
Member

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.

jabelloc

jabelloc commented on Apr 20, 2018

@jabelloc
Author

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.

jwakely

jwakely commented on Apr 20, 2018

@jwakely
Member

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

jwakely commented on Apr 20, 2018

@jwakely
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jwakely@jabelloc

        Issue actions

          The example in [over.sub]p1 is incorrect · Issue #2042 · cplusplus/draft