blob: 9d3c05e26b1c72b3c5a8a1e0037339b45ee4c09c [file] [log] [blame]
Howard Hinnantf619e232013-01-11 20:36:59 +00001//===----------------------------------------------------------------------===//
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// <list>
11
12// void push_back(const value_type& x);
13
14#include <list>
15#include <cassert>
16
17// Flag that makes the copy constructor for CMyClass throw an exception
18static bool gCopyConstructorShouldThow = false;
19
20
21class CMyClass {
Howard Hinnant171771a2013-07-08 21:06:38 +000022 public: CMyClass();
23 public: CMyClass(const CMyClass& iOther);
24 public: ~CMyClass();
Howard Hinnantf619e232013-01-11 20:36:59 +000025
Howard Hinnant171771a2013-07-08 21:06:38 +000026 private: int fMagicValue;
Howard Hinnantf619e232013-01-11 20:36:59 +000027
Howard Hinnant171771a2013-07-08 21:06:38 +000028 private: static int kStartedConstructionMagicValue;
29 private: static int kFinishedConstructionMagicValue;
Howard Hinnantf619e232013-01-11 20:36:59 +000030};
31
32// Value for fMagicValue when the constructor has started running, but not yet finished
33int CMyClass::kStartedConstructionMagicValue = 0;
34// Value for fMagicValue when the constructor has finished running
35int CMyClass::kFinishedConstructionMagicValue = 12345;
36
37CMyClass::CMyClass() :
Howard Hinnant171771a2013-07-08 21:06:38 +000038 fMagicValue(kStartedConstructionMagicValue)
Howard Hinnantf619e232013-01-11 20:36:59 +000039{
Howard Hinnant171771a2013-07-08 21:06:38 +000040 // Signal that the constructor has finished running
41 fMagicValue = kFinishedConstructionMagicValue;
Howard Hinnantf619e232013-01-11 20:36:59 +000042}
43
44CMyClass::CMyClass(const CMyClass& /*iOther*/) :
Howard Hinnant171771a2013-07-08 21:06:38 +000045 fMagicValue(kStartedConstructionMagicValue)
Howard Hinnantf619e232013-01-11 20:36:59 +000046{
Howard Hinnant171771a2013-07-08 21:06:38 +000047 // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
48 if (gCopyConstructorShouldThow) {
49 throw std::exception();
50 }
51 // Signal that the constructor has finished running
52 fMagicValue = kFinishedConstructionMagicValue;
Howard Hinnantf619e232013-01-11 20:36:59 +000053}
54
55CMyClass::~CMyClass() {
Howard Hinnant171771a2013-07-08 21:06:38 +000056 // Only instances for which the constructor has finished running should be destructed
57 assert(fMagicValue == kFinishedConstructionMagicValue);
Howard Hinnantf619e232013-01-11 20:36:59 +000058}
59
60int main()
61{
Howard Hinnant171771a2013-07-08 21:06:38 +000062 CMyClass instance;
63 std::list<CMyClass> vec;
Howard Hinnantf619e232013-01-11 20:36:59 +000064
Howard Hinnant171771a2013-07-08 21:06:38 +000065 vec.push_back(instance);
Howard Hinnantf619e232013-01-11 20:36:59 +000066
Howard Hinnant171771a2013-07-08 21:06:38 +000067 gCopyConstructorShouldThow = true;
68 try {
69 vec.push_back(instance);
70 }
71 catch (...) {
72 }
Howard Hinnantf619e232013-01-11 20:36:59 +000073}