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;
23 public: CMyClass(const CMyClass& iOther);
26 private: int fMagicValue;
28 private: static int kStartedConstructionMagicValue;
29 private: static int kFinishedConstructionMagicValue;
32 // Value for fMagicValue when the constructor has started running, but not yet finished
33 int CMyClass::kStartedConstructionMagicValue = 0;
34 // Value for fMagicValue when the constructor has finished running
35 int CMyClass::kFinishedConstructionMagicValue = 12345;
37 CMyClass::CMyClass() :
38 fMagicValue(kStartedConstructionMagicValue)
40 // Signal that the constructor has finished running
41 fMagicValue = kFinishedConstructionMagicValue;
44 CMyClass::CMyClass(const CMyClass& /*iOther*/) :
45 fMagicValue(kStartedConstructionMagicValue)
47 // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
48 if (gCopyConstructorShouldThow) {
49 throw std::exception();
51 // Signal that the constructor has finished running
52 fMagicValue = kFinishedConstructionMagicValue;
55 CMyClass::~CMyClass() {
56 // Only instances for which the constructor has finished running should be destructed
57 assert(fMagicValue == kFinishedConstructionMagicValue);
63 std::vector<CMyClass> vec;
65 vec.push_back(instance);
67 gCopyConstructorShouldThow = true;
69 vec.push_back(instance);