Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does --list.end() compile?

  • list's end() returns a copy of the past-the-end iterator, right?
  • Therefore, list.end() is an rvalue, right?
  • the -- operator-function overloaded for list iterator takes a non-const reference, right?
  • you can't bind rvalues to non-const references, right?

So how come does

std::list<int> lst;
// ... 
--l.end();` 

compile?

As correctly pointed out, my third point is not necessarily right. But then how about this code which also compiles?

struct A{};

void f(A&)
{

}
A a()
{
    return A();
}

int main()
{
    f(a());
}
like image 777
Armen Tsirunyan Avatar asked Feb 27 '13 16:02

Armen Tsirunyan


1 Answers

  • the -- operator-function overloaded for list iterator takes a non-const reference, right?

This point is wrong. The operator-- is a member function, and a member function can be invoked on a temporary. You don't pass any object to this member function as argument. so the question of binding rvalue to non-const reference doesn't arise in the first place.


As for the edit (passing rvalue to f(A&){}), it is a non-standard extension. I guess you're using Microsoft Compiler, because I know it has this non-standard extension which is stupid in my opinion.

like image 63
Nawaz Avatar answered Oct 01 '22 04:10

Nawaz