Why can't we implement both the `getAB () &&` and `getAB ()` methods?

Why can't we implement both methods getAB() &&and getAB(), but can we implement either of them?

code:

struct Beta {
  Beta_ab ab;
  Beta_ab && getAB() && { cout << "1"; return move(ab); }
};

int main() {    
    Beta_ab ab = Beta().getAB();

    return 0;
}

  1. 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;
}

  1. Doen't working: 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 do the first two code examples work, but the last example does not work?

+6
source share
1 answer

Standard section [over.load] /2.3:

- , - , , - , , ref-.

[:

class Y {
  void h() &;
  void h() const &;    // OK
  void h() &&;         // OK, all declarations have a ref-qualifier
  void i() &;
  void i() const;      // ill-formed, prior declaration of i
                       // has a ref-qualifier
};

- ]

, , . ( , - , .)

: ref- lvalue (&) "2", rvalues, lvalues.

+8

All Articles