mardi 24 août 2021

How does std::bind Results in calling the Copy Constructor Several Times

I have been trying to understand how std::bind works. So working up with different examples. Below is the sample program whose output i am unable to understand.

VERSION 1

class NAME
{
  public:
    void f()
    {
        std::cout<<"f"<<std::endl;
    }
    NAME()
    {
        std::cout<<"default constructor"<<std::endl;
    }
    NAME(const NAME&)
    {
        std::cout<<"copy constructor"<<std::endl;
    }
};
int main()
{
   std::cout << "Hello World" << std::endl; 
   NAME n;
   std::function<void ()> callable = std::bind(&NAME::f, n);
   
   
   return 0;
}

The output of the above version 1 is as follows:

Hello World
default constructor
copy constructor
copy constructor

I know that the argument passed will be copied, so the copy constructor should be called only one time but in the output above the copy constructor is called twice. Why/how is this happening? Is it because the new callable that is created using std::bind will be used to initialize another callable using std::function on the lhs?

VERSION 2

int main()
{
   std::cout << "Hello World" << std::endl; 
   NAME n;
   std::function<void ()> callable = std::move(std::bind(&NAME::f, n));
   return 0;
}

The output of VERSION 2 is as follows:

Hello World
default constructor
copy constructor
copy constructor
copy constructor

In the output above(for version 2) when i use std::move, why/how is the copy constructor called thrice?

VERSION 3

int main()
{
   std::cout << "Hello World" << std::endl; 
   NAME n;
   auto callable = std::bind(&NAME::f, n);
   return 0;
}

The output of version 3 is as follows:

Hello World
default constructor
copy constructor

In this case(version 3) why/how is the copy constructor called only once?

VERSION 4

int main()
{
   std::cout << "Hello World" << std::endl; 
   NAME n;
   auto callable = std::move(std::bind(&NAME::f, n));
   return 0;
}

The output of version 4 is as follows:

Hello World
default constructor
copy constructor
copy constructor

What happens in this case(version 4) when we use auto and std::move(), why/how is the copy constructor called twice.

PS: The program is executed on an online compiler: Online compiler Used

Aucun commentaire:

Enregistrer un commentaire