c++ - Using std::move for a "take" method implementation -
i implement "take" method. "take" method method, steals getted object owner: owner left object in empty state. of course, involves c++11 move semantics. of following best way implement it?
class b { public: take_a_1() { return std::move(m_a); } // can compiled looks weird me std::move // not complain i'm passing constant object take_a_2() const { return std::move(m_a); } a&& take_a_3() { return std::move(m_a); } // can't compiled (which fine) // // a&& take_a_4() const // { // return std::move(m_a); // } private: m_a; };
none of them. i'd use this:
struct b { && get() && { return std::move(m_a); } };
that way can "take" rvalues of b
:
b b; a = std::move(b).get();
Comments
Post a Comment