0

My assumption was that std::auto_ptr can not be used in standard containers ( Why is it wrong to use std::auto_ptr<> with standard containers? ), giving the compiler error because of only copy semantics was supported before C++11, but I think we can use auto_ptr same as std::unique_ptr in the container, if we use move semantics with out any issues.

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

int main()
{
  auto_ptr<int> a1(new int(1));
  auto_ptr<int> a2(new int(2));
  auto_ptr<int> a3(new int(3));
  auto_ptr<int> a4(new int(4));

  vector<auto_ptr<int>> v1;
  v1.push_back(std::move(a1));
  v1.push_back(std::move(a2));
  v1.push_back(std::move(a3));
  v1.push_back(std::move(a4));

  for(auto it: v1)
    cout <<*it <<" ";
}
Dean Seo
  • 5,486
  • 3
  • 30
  • 49
Alok
  • 1,997
  • 2
  • 18
  • 30

0 Answers0