Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 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 | // <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 |
| 18 | static bool gCopyConstructorShouldThow = false; |
| 19 | |
| 20 | |
| 21 | class CMyClass { |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 22 | public: CMyClass(); |
| 23 | public: CMyClass(const CMyClass& iOther); |
| 24 | public: ~CMyClass(); |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 25 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 26 | private: int fMagicValue; |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 27 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 28 | private: static int kStartedConstructionMagicValue; |
| 29 | private: static int kFinishedConstructionMagicValue; |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 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; |
| 36 | |
| 37 | CMyClass::CMyClass() : |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 38 | fMagicValue(kStartedConstructionMagicValue) |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 39 | { |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 40 | // Signal that the constructor has finished running |
| 41 | fMagicValue = kFinishedConstructionMagicValue; |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | CMyClass::CMyClass(const CMyClass& /*iOther*/) : |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 45 | fMagicValue(kStartedConstructionMagicValue) |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 46 | { |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 47 | // 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 Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | CMyClass::~CMyClass() { |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 56 | // Only instances for which the constructor has finished running should be destructed |
| 57 | assert(fMagicValue == kFinishedConstructionMagicValue); |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | int main() |
| 61 | { |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 62 | CMyClass instance; |
| 63 | std::list<CMyClass> vec; |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 64 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 65 | vec.push_back(instance); |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 66 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame^] | 67 | gCopyConstructorShouldThow = true; |
| 68 | try { |
| 69 | vec.push_back(instance); |
| 70 | } |
| 71 | catch (...) { |
| 72 | } |
Howard Hinnant | f619e23 | 2013-01-11 20:36:59 +0000 | [diff] [blame] | 73 | } |