Why can't we implement both methods getAB() &&
and getAB()
, but can implement any one of these?
- Works: http://ideone.com/4EgObJ
Code:
struct Beta {
Beta_ab ab;
Beta_ab && getAB() && { cout << "1"; return move(ab); }
};
int main() {
Beta_ab ab = Beta().getAB();
return 0;
}
- Works: http://ideone.com/m9d0Tz
Code:
struct Beta {
Beta_ab ab;
Beta_ab && getAB() { cout << "2"; return move(ab); }
};
int main() {
Beta b;
Beta_ab ab = b.getAB();
return 0;
}
- Doen't works: http://ideone.com/QIQtZ5
Code:
struct Beta {
Beta_ab ab;
Beta_ab && getAB() && { cout << "1"; return move(ab); }
Beta_ab && getAB() { cout << "2"; return move(ab); }
};
int main() {
Beta b;
Beta_ab ab1 = b.getAB();
Beta_ab ab2 = Beta().getAB();
return 0;
}
Why are the first two examples of code works, but the last example does not work?
ab
in themain
function. – AlgonquianBeta_ab && getAB() & { cout << "2"; return move(ab); }
. (Not posting as an answer because this is surely a dupe.) – EssayistBeta_ab && getAB()
, may be called on an lvalue or an rvalue. An overload with rvalue ref qualifier,Beta_ab && getAB() &&
, may be called only on rvalues. Therefore, if both were allowed to coexist, the call ofgetAB()
on an rvalue would be ambiguous. – Nursery