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_front(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 {
22    public: CMyClass();
23    public: CMyClass(const CMyClass& iOther);
24    public: ~CMyClass();
25
26    private: int fMagicValue;
27
28    private: static int kStartedConstructionMagicValue;
29    private: static int kFinishedConstructionMagicValue;
30};
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() :
38    fMagicValue(kStartedConstructionMagicValue)
39{
40    // Signal that the constructor has finished running
41    fMagicValue = kFinishedConstructionMagicValue;
42}
43
44CMyClass::CMyClass(const CMyClass& /*iOther*/) :
45    fMagicValue(kStartedConstructionMagicValue)
46{
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;
53}
54
55CMyClass::~CMyClass() {
56    // Only instances for which the constructor has finished running should be destructed
57    assert(fMagicValue == kFinishedConstructionMagicValue);
58}
59
60int main()
61{
62    CMyClass instance;
63    std::list<CMyClass> vec;
64
65    vec.push_front(instance);
66
67    gCopyConstructorShouldThow = true;
68    try {
69        vec.push_front(instance);
70    }
71    catch (...) {
72    }
73}
74