Home | History | Annotate | Download | only in list.modifiers
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // UNSUPPORTED: libcpp-no-exceptions
     11 // <list>
     12 
     13 // void push_back(const value_type& x);
     14 
     15 #include <list>
     16 #include <cassert>
     17 
     18 // Flag that makes the copy constructor for CMyClass throw an exception
     19 static bool gCopyConstructorShouldThow = false;
     20 
     21 
     22 class CMyClass {
     23     public: CMyClass();
     24     public: CMyClass(const CMyClass& iOther);
     25     public: ~CMyClass();
     26 
     27     private: int fMagicValue;
     28 
     29     private: static int kStartedConstructionMagicValue;
     30     private: static int kFinishedConstructionMagicValue;
     31 };
     32 
     33 // Value for fMagicValue when the constructor has started running, but not yet finished
     34 int CMyClass::kStartedConstructionMagicValue = 0;
     35 // Value for fMagicValue when the constructor has finished running
     36 int CMyClass::kFinishedConstructionMagicValue = 12345;
     37 
     38 CMyClass::CMyClass() :
     39     fMagicValue(kStartedConstructionMagicValue)
     40 {
     41     // Signal that the constructor has finished running
     42     fMagicValue = kFinishedConstructionMagicValue;
     43 }
     44 
     45 CMyClass::CMyClass(const CMyClass& /*iOther*/) :
     46     fMagicValue(kStartedConstructionMagicValue)
     47 {
     48     // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
     49     if (gCopyConstructorShouldThow) {
     50         throw std::exception();
     51     }
     52     // Signal that the constructor has finished running
     53     fMagicValue = kFinishedConstructionMagicValue;
     54 }
     55 
     56 CMyClass::~CMyClass() {
     57     // Only instances for which the constructor has finished running should be destructed
     58     assert(fMagicValue == kFinishedConstructionMagicValue);
     59 }
     60 
     61 int main()
     62 {
     63     CMyClass instance;
     64     std::list<CMyClass> vec;
     65 
     66     vec.push_back(instance);
     67 
     68     gCopyConstructorShouldThow = true;
     69     try {
     70         vec.push_back(instance);
     71     }
     72     catch (...) {
     73     }
     74 }
     75