1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // void push_back(const value_type& x);
17 // Flag that makes the copy constructor for CMyClass throw an exception
18 static bool gCopyConstructorShouldThow = false;
22 public: CMyClass(int tag);
23 public: CMyClass(const CMyClass& iOther);
26 bool equal(const CMyClass &rhs) const
27 { return fTag == rhs.fTag && fMagicValue == rhs.fMagicValue; }
32 private: static int kStartedConstructionMagicValue;
33 private: static int kFinishedConstructionMagicValue;
36 // Value for fMagicValue when the constructor has started running, but not yet finished
37 int CMyClass::kStartedConstructionMagicValue = 0;
38 // Value for fMagicValue when the constructor has finished running
39 int CMyClass::kFinishedConstructionMagicValue = 12345;
41 CMyClass::CMyClass(int tag) :
42 fMagicValue(kStartedConstructionMagicValue), fTag(tag)
44 // Signal that the constructor has finished running
45 fMagicValue = kFinishedConstructionMagicValue;
48 CMyClass::CMyClass(const CMyClass& iOther) :
49 fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag)
51 // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
52 if (gCopyConstructorShouldThow) {
53 throw std::exception();
55 // Signal that the constructor has finished running
56 fMagicValue = kFinishedConstructionMagicValue;
59 CMyClass::~CMyClass() {
60 // Only instances for which the constructor has finished running should be destructed
61 assert(fMagicValue == kFinishedConstructionMagicValue);
64 bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); }
68 CMyClass instance(42);
69 std::deque<CMyClass> vec;
71 vec.push_back(instance);
72 std::deque<CMyClass> vec2(vec);
74 gCopyConstructorShouldThow = true;
76 vec.push_back(instance);