+
+struct CountedWidget : Widget {
+ static std::vector<Widget*> instances_;
+ bool alive = true;
+ /* implicit */ CountedWidget(int v) : Widget(v) {
+ instances_.push_back(this);
+ }
+ CountedWidget(const CountedWidget& other) : Widget(other) {
+ instances_.push_back(this);
+ }
+ CountedWidget(CountedWidget&& other) noexcept(false)
+ : Widget(std::move(other)) {
+ other.alive = false;
+ other.remove();
+ instances_.push_back(this);
+ }
+ ~CountedWidget() {
+ if (alive) {
+ remove();
+ }
+ }
+
+ private:
+ CountedWidget& operator=(const CountedWidget&) = delete;
+ CountedWidget& operator=(CountedWidget&&) = delete;
+
+ void remove() {
+ auto iter = std::find(instances_.begin(), instances_.end(), this);
+ EXPECT_TRUE(iter != instances_.end());
+ instances_.erase(iter);
+ }
+};
+
+std::vector<Widget*> CountedWidget::instances_;