DigestCPP

Lets Understand With Example

  • Home
  • Design Principal
  • Design Patterns
  • C++ 11 Features
  • C++11 Multithreading
  • Contact Us

std::move c++11

std::move

It is library function and its function template. It automatically casts its argument into rvalue. rvalue is perfect candidate for move operation (that is done by either move constructor or move assignment operator (both take rvalue object only)) so std::move makes an object eligible for move operation.

Example:

std::string s11{"digestcpp.com"};
std::string s22{std::move(s11)};
std::cout<<"s22:"<<s22<<std::endl;
std::cout<<"s11:"<<s11<<std::endl;
Output:
s22:digestcpp.com
s11: //Its empty now,object has been moved.

Customised Own make() function for string:

//Own Move function
mystring&& mymove(mystring & tmp)
{
    std::cout<<"MyMove function"<<std::endl;
    return static_cast<mystring&&>(tmp);
}

When will we use it (What is the Objective/purpose of std::move):

  • When we want to do move operation on lvalue object. Move semantics (move constructor and move assignment) takes only rvalue as parameter so we need to convert lvalue to rvalue for move operation, this conversion will be done by std::move.

Advantages:

  • Some time copy operations are very expensive then we can do move operation but move semantics take only rvalue as parameter then this conversion will be taken care by std::move.

source code with example:

/*
Program:std::move 
Author: Alpha Master
Date: 7 March 2021
*/

//Header File
#include<iostream>
#include<string.h>

class mystring;

//Move function
mystring&& mymove(mystring & tmp)
{
    std::cout<<"MyMove function"<<std::endl;
    return static_cast<mystring&&>(tmp);
}

//My String with all default functionlities
class mystring
{
    char* mpStr;
public:
    mystring(char* tmp = nullptr); //default & parameterized constructor
    ~mystring();  //destructor
    mystring(mystring&&);    //move contructor
    //mystring&& mymove(mystring&);   //mymove 
    //Function to print
    friend std::ostream &operator<<(std::ostream &output, const mystring& tmp ) 
    {
         if(tmp.mpStr != nullptr)
         {
             output <<"mystring :" << tmp.mpStr << std::endl;
         }
         else
         {
            output <<"mystring : Its empty" << std::endl;
         }
         return output;
    }

};


mystring::mystring(char* tmp)
{
    std::cout<<"default & parameterised constructor"<<std::endl;
    if(tmp != nullptr)
    {
        mpStr = new char[strlen(tmp) + 1];
        strcpy(mpStr, tmp);
    }
}

mystring::~mystring()
{
    //std::cout<<"destructor"<<std::endl;
    if(mpStr)
    {
        delete[] mpStr;
    }
}


mystring:: mystring(mystring&& tmp)
{
    std::cout<<"move constructor"<<std::endl;
    mpStr = tmp.mpStr;
    tmp.mpStr = nullptr;
}


int main()
{

    mystring s11("digestcpp.com");//param constructor
    std::cout<<s11;

    mystring s14(mymove(s11)); //move constructor
    std::cout<<s14;
    std::cout<<"s11 is moved,so expecting empty"<<std::endl; 
    std::cout<<s11;
    
    return 0;
}

Output:

default & parameterised constructor
mystring :digestcpp.com
MyMove function
move constructor
mystring :digestcpp.com
s11 is moved,so expecting empty
mystring : Its empty

Primary Sidebar




DigestCPP © 2023. All rights reserved.

    About Privacy Policy Terms and Conditions Contact Us Disclaimer