SHARE
TWEET

Untitled

a guest Oct 12th, 2017 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct counted
  2. {
  3.     counted() : data()
  4.     {
  5.         auto p = instances.insert(this);
  6.         if (!(p.second == true))
  7.         cout << "WRONG1" << endl;
  8.     }
  9.     counted(int data)
  10.         : data(data)
  11.     {
  12.         // cerr << "HERE insert " << data << endl;
  13.         auto p = instances.insert(this);
  14.         cerr << "HERE insert " << data << " " << instances.size() << endl;
  15.         if (!(p.second == true))
  16.         cout << "WRONG2" << endl;
  17.     }
  18.  
  19.     counted(counted const& other)
  20.         : data(other.data)
  21.     {
  22.         cerr << "-? " << instances.size() << endl;
  23.         // for(auto i : instances)
  24.         //     cerr << i->data << " ";
  25.         // cout << endl;
  26.         auto p = instances.insert(this);
  27.         // cerr << "-? " << instances.size() << endl;
  28.         // for(auto i : instances)
  29.         //     cerr << i->data << " ";
  30.         // cout << endl;
  31.         if (!(p.second == true))
  32.             {cout << data << " WRONG3" << endl;}
  33.     }
  34.  
  35.     ~counted()
  36.     {
  37.         // cerr << "CALLED " << instances.size() << "\n";
  38.         size_t n = instances.erase(this);
  39.         if (!(1u == n))
  40.         cout << "WRONG4" << endl;
  41.     }
  42.  
  43.     counted& operator=(counted const& c)
  44.     {
  45.         if (!((instances.find(this) != instances.end()) == true))
  46.         cout << "WRONG5" << endl;
  47.  
  48.         data = c.data;
  49.         return *this;
  50.     }
  51.  
  52.     operator int() const
  53.     {
  54.         if (!((instances.find(this) != instances.end()) == true))
  55.         cout << "WRONG6" << endl;
  56.  
  57.         return data;
  58.     }
  59.  
  60.     static void expect_no_instances()
  61.     {
  62.         // EXPECT_EQ(0u, instances.size());
  63.         if (!(0u == instances.size()))
  64.             cout << "WRONG7" << endl;
  65.     }
  66. private:
  67.     int data;
  68.     static set<counted const*> instances;
  69. };
  70.  
  71. std::set<counted const*> counted::instances;
  72.  
  73. using container = bimap<counted, counted>;
RAW Paste Data
Top