8

While learning about iterators in C++ I tried the following:

#include <vector>

int main() {
    std::vector<int> a;
    a.end()=a.begin();
    //Why is this even allowed by the compiler?
}

What am I missing?

2
  • 1
    Why should that specifically be forbidden? And what do you think that line does?
    – Mat
    Commented Sep 21, 2014 at 11:16
  • 3
    Well, as far as I know if I do the same with a member function of a class the compiler complains because it must be a modifiable lvalue.
    – 4nt
    Commented Sep 21, 2014 at 11:22

5 Answers 5

10

it would not be posiible if for example function end would return a pointer.

For example this code will not be compiled

int a[] = { 1, 2, 3 };

std::end( a ) = std::begin( a );

GCC issues error

error: lvalue required as left operand of assignment
  std::end( a ) = std::begin( a );
                ^

However when objects of class types are used then they can call the ovetloaded copy (or move) assignment operator.

So the question arises why do not the functions return constant iterator objects. I think that it would be possible to apply operator ++. For example

auto it = ++c.begin();

For direct access iterators as pointers you can write simply

auto it = begin( a ) + 1;
0
7

The standard doesn't specify whether std::vector::iterator is a class type, or a raw pointer.

If it is a class type then this code calls operator= on the temporary object returned by a.end(). Not a very useful operation, but legal. (Rvalues may have functions called on them).

If your library makes std::vector::iterator be a pointer then this code would fail to compile, since simple assignment requires an lvalue on the left.

Jarod42 pointed out that if the iterator's assignment operator had been defined as:

std::vector::iterator& std::vector::iterator::operator =(std::vector::iterator) &;

then this code would be illegal; the trailing & means that the function is only selectable when it is being called on an lvalue.

But it wasn't defined that way, I would guess that the Committee didn't want to make some legal code illegal for no good reason; maybe there is a use case for it that nobody thought of yet.

1
  • re "I would guess that the Committee didn't want to make some legal code illegal for no good reason", much in C++ does not have any specific reason. but it's a practical language, so things that clearly did not work have been shaved off. since the rules here permit code that may compile with one compiler and not with another, a reasonable conclusion is just that that's never been a significant problem: it's not been shaved off. Commented Sep 21, 2014 at 12:27
3

The line affect a temporary iterator, and so is useless.

std::vector::iterator = std::vector::iterator is allowed.

A way to disallow that would be to have

std::vector::iterator::operator =(std::vector::iterator) &; // note the extra &

but std::vector::iterator may be a simple pointer, and we can't disalow T* = T*

4
  • 2
    Can you explain exactly what does the & at the end of the function declaration do?
    – Svalorzen
    Commented Sep 21, 2014 at 11:26
  • 1
    @Svalorzan it means that the function is only a viable candidate when called on an lvalue
    – M.M
    Commented Sep 21, 2014 at 11:29
  • Plus, ref qualifiers were introduced into the language waaaay later than STL was.
    – shakurov
    Commented Sep 21, 2014 at 11:39
  • 2
    re "we can't disalow T* = T*", that's already invalid for temporaries, which is likely the source of the OP's confusion. Commented Sep 21, 2014 at 12:22
2

You cannot assign to an rvalue (aka temporary) primitive type in C++, like (int)7 or a pointer returned by-value from a function. This makes lots of sense, as the result of the assignment would be immediately discarded, so it is probably not what the programmer wanted. rvalue means 'right hand side of the = sign' from language grammars, or a temporary (anonymous) value.

However on objects of class type, = just invokes operator=. Usually when the lhs is an rvalue, this would make no sense, but sometimes it would (say, the class is a proxy object pseudo-reference (like what [] on std::vector<bool> returns). So it is treated nit at all special, it just invokes the method. And there was no way to tell if the method was invoked from a temporary rvalue or a 'real' lvalue.

rvalue references are new in C++11, as are rvalue qualified methods.

Prior to C++11, there was no way to block calling operator= on an object of class type just because it was a temporary.

As begin returns a non-const temporary, if it is of class type (not guaranteed by the standard) it can be assigned to.

Today we can block assignment to temporaries of class type, but the standard library has not been modified to block it on its types. Probably at least partly to avoid backwards compwtibility issues, and partly to determine best practices outside std first.

4
  • 1
    Actually a.begin() = a.end() is possible on prior C++11 as well. Commented Sep 21, 2014 at 13:30
  • @nipun I said it was not possible to block it only for rvalues prior to C++11. Or did I miss a negation? Commented Sep 21, 2014 at 16:06
  • I think I haven't understand correctly what you are pointing to. But we could always block operator= by making it private. Now, with c++11 we have deleted member functions which is an even easier way to block it. Commented Sep 21, 2014 at 16:23
  • 1
    @nipun You can block it, but not only for rvalues (prior to C++11). delete is irrelevant. In C++11 we can detect if the this object is being invoked in an rvalue context, and selectively block it. This allows = in general, but blocks it when the lhs is an rvalue, just like primitive types. Commented Sep 21, 2014 at 16:26
1

They are allowed because the iterator objects are copy-assignable and copy-constructible. That is why we can do something like a.begin() = a.end(). Also, we can do something auto it(a.begin()).

Check the examples below regarding copy-assignable.

#include <vector>

struct CopyConsAssignable
{
    int x;
    CopyConsAssignable(int a) 
    { 
        x = a; 
    }

    CopyConsAssignable(const CopyConsAssignable& other)
    {
        x = other.x;
    }

    CopyConsAssignable& operator=(const CopyConsAssignable& other)
    {
        if (this != &other)
        {
            x = other.x;
        }
        return *this;
    }
};


int main()
{
    std::vector<int> a;
    a.begin() = a.end();

    CopyConsAssignable(2) = CopyConsAssignable(4); // Allowed as the objects are copy-assignable
    return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.