On this link on comp.std.c++, Mr. Howard Hinnant shows the following code:
#include <utility>
#include <cassert>
struct A
{
A() : data_(1) {}
A(A&& a) : data_(a.data_) {a.data_ = 0;}
int data_;
};
void g(const A&) {}
void g(A&& a) {a.data_ = 0;}
void h(const A&) {}
void h(A&& a) {a.data_ = 0;}
void f(A&& a)
{
g(a); // calls g(const A&)
h(a); // calls h(const A&)
if (true)
{
h(a); // calls h(A&&) (by Alexandrescu rules)
}
}
int main()
{
A a;
f(a);
assert(a.data_ == 1);
}
Then he writes:
By the N1377 rules, it does not assert. ...
With the actual C++11 rules the code above doesn't compile because the lvalue a
in main()
doesn't bind to an rvalue reference. But just assuming it compiled, as probably was supposed to the case when this discussion occurred, I can't understand the assertion above, that is, By the N1377 rules, it does not assert.
. According to the prior rules, wouldn't the variable a
be pilfered (a.data_ = 0), as a
is passed as an argument to f
with f(a) ?