C ++ - rvalue references

1. Bind a value to the right will be destroyed references - Mobile

2. lvalue expression - the identity of the object; the value of the right expression - the value of the object

int I = 42 is ;
 int & R & lt = I; 

int && RR = I; // ×, can not bind to the value of the left and right reference value 
int & R2 = I * 42 is ; // ×, left and right values can not bind to the value reference 

const  int & R3 = I * 42 is ; // √, rvalue can bind const reference 
int && * I RR2 = 42 is ; // rvalue rvalue reference to bind 

int && RR1 = 42 is ; // 
int && RR1 = RR2; // × 

int std :: && RR3 = Move (RR1); //
√, use std avoid potential name conflicts
 

 

3. Persistence left and right values ​​short (usually literal, or expression creates temporary objects (above)) // variable life long, a} will be destroyed

4. Right Object reference value is about to be destroyed and that the object has no other users

5. Move the constructor and move assignment operator

5.1 Mobile new constructor does not allocate any memory, taking over a given memory, after taking over the original object is set to null

StrVec (StrVec && s) noexcept: Elements (s.elements), first_free (s.first_free), CAP (s.cap) // resource takeover of s 
{ 
    s.elements = s.first_free = s.cap = nullptr a; // let s into the null, or when the destruction s, construction will be destroyed 
}

5.2 Mobile assignment operator

& StrVec operator = (StrVec && rhs) noexcept // why other places do not need? 
    { 
        IF ( the this = & RHS!) // detection self-assignment 
        {
             Free (); 
            Elements = rhs.elements; 
            first_free = rhs.first_free; 
            CAP = rhs.cap; 
            rhs.elements = rhs.first_free = rhs.cap = nullptr a; 
        } 
        return * the this ; 
    }

5.3 Synthesis move operation:

  Class defines its own copy constructor, assignment operator copies or destructor, the synthesis will not move constructor and move assignment operator. Only when a class is not defined in any of their versions of copy control members, each non-static data members of the class, and can be moved, the compiler will assignment operator constructor for combined moving or moved.

struct X-{
     int I;     // built-in type, can be moved 
    String S; // String moving operation has defined 
}; 

struct hasX { 
    X-Men; // X-moving operation synthetic 
}; 

X-X, X2 = STD :: move (X); // synthetic mobile constructor 
hasX HX, HX2 STD :: = move (HX);     // synthetic moved constructor

5.4 defines a class constructor or a moving mobile assignment operator must define your own copy operation. Otherwise, these members will be delete the

6. Move the right value, the value of the left copy. However, if the constructor does not move, the right value is also copied.

7. A class has available a copy constructor, the constructor does not move, the object through the copy constructor to "move". Similarly, suitable for moving and copying assignment operator assignment operators.

 

class StrVec {
 public : 
    StrVec (): Elements (nullptr a), first_free (nullptr a), CAP (nullptr a) {} // default initialization 
    StrVec ( const StrVec &); // copy constructor 
    StrVec & operator = ( const StrVec &); // copy assignment operator 
    ~ StrVec (); 

    StrVec (StrVec && s) noexcept: Elements (s.elements), first_free (s.first_free), CAP (s.cap) // resource takeover of s 
    { 
        s.elements = s.first_free s.cap nullptr a = =; // make s into the null 
    } 
    StrVec & operator = (&& StrVec rhs) noexcept
    {
        if (this != &rhs)//检测自赋值
        {
            free();
            elements = rhs.elements;
            first_free = rhs.first_free;
            cap = rhs.cap;
            rhs.elements = rhs.first_free = rhs.cap = nullptr;
        }
        return *this;
    }

    void push_back(const string&);//拷贝元素
    void push_back(string&&);//移动元素,右值引用
    string* begin() const { return elements; }
    string* end() const { return first_free; }

    size_t size() const { return first_free - elements; }
    size_t capacity() const { return cap - elements; }
private:
    static std::allocator<std::string> alloc;//分配器
    void chk_n_alloc() { if (size() == capacity()) reallocate(); }//Each additional element must Check 

    pair < String *, String *> alloc_n_copy ( const  String *, const  String *); // tools to copy constructor, assignment operator and destructor with 
    void Reallocate ();                 // get a more Large memory and copy elements 

    void  as free ();         // destroy free up memory     

    std :: String * elements;         // pointer to an array of elements 
    std :: String * first_free;     // pointer to the first element of a free 
    std :: String CAP *;             // pointer to an array after the end position 
}; 

void:: push_back StrVec ( const  String & S) 
{ 
    chk_n_alloc (); // ensure enough space to accommodate the new element 
    alloc.construct (first_free ++ , S); 
} 

void StrVec :: push_back ( String && S) 
{ 
    chk_n_alloc (); / / secure a space enough to accommodate the new element 
    alloc.construct (first_free ++ , STD :: Move (S)); 
} 

StrVec VEC;
String S = "some Thing";
vec.push_back (S); // call
push_back (const string & s )
vec.push_back ( "DONE"); // call push_back (string && s)
 

8. left and right reference value of the member function value

8.1 How to modify rvalue reference member function, what is the meaning, how to call

class Foo 
{ 
    Foo & operator = ( const Foo & RHS) & 
    { 
        // RHS is assigned to the object of the present 
        return * the this ; // leftwardly value 
    } 
    Foo the sorted () && ; 
    Foo the sorted () const & ;
 Private : 
    List < int > Data; 
}; 
Foo the sorted :: Foo () && // this object is rvalue, no other users can sort the source object 
{ 
    Sort (data.begin (), data.end ()); 
    return * the this ; 
} 
foo foo :: sorted ()const & // This object is left value, only the first copy, the reordering can not change the original 
{ 
    Foo RET ( * the this ); 
    Sort (ret.data.begin (), ret.data.end ()) ; 
    return RET; 
}

8.2 define two or more members with the same name as a function parameter list, or add a reference qualifiers are either not increase

    The sorted foo () &&; // . 1 
    foo the sorted () const &; // 2 are added (if it is not & ×) 
    the using Comp = BOOL ( const  int &, const  int &); // COM is a function - alias 
    Foo the sorted (Comp *); // . 3 
    Foo the sorted (Comp *) const ; // . 4 do not increase (if it & ×)

 

Guess you like

Origin www.cnblogs.com/yrm1160029237/p/11575539.html