mardi 5 février 2019

emplace_back is not creating inplace object

    #include <iostream>
    #include<vector>
    using namespace std;

    class test{
        public:
        test(){
        cout<<"constructor called"<<endl;
    }
     test(const test& obj){
      cout<<"copy constructor called"<<endl;
     }
     test(test&& obj){
      cout<<"Move constructor called"<<endl;
    }     
    };
  int main()
  {
      vector<test> vec;
      vec.emplace_back(test());

      return 0;
  }

when i run above program I expected emplace_back to create object in vector in place.Thus "constructor called" should have been output since emplace_back would avoid creating temporary object.But the output is: constructor called Move constructor called

Here, temporary object is created just like push_back. Please explain.

Aucun commentaire:

Enregistrer un commentaire