#include #include #include template int count(); template class Tracker : public T { public: template Tracker(Args&&... args) : T(std::forward(args)...) { ++counter; } ~Tracker() { --counter; } Tracker(Tracker const& other) : T{other} { ++counter; } Tracker(Tracker&&) = default; Tracker& operator=(Tracker const& other) { Tracker tmp{other}; return *this = std::move(tmp); } private: static int counter; friend int count(); }; template int Tracker::counter{0}; template int count() { return Tracker::counter; } struct Hello_Worlder { std::string get() { return "hello world!"; } }; /* The output should be: 0 str1 str2 hello world! ++++++++++ 3 1 2 */ int main() { using std::cout; using std::endl; using std::string; cout << count() << endl; Tracker str1{}; str1 += "str1"; for (char c : str1) { cout << c; } Tracker str2{"str2"}; cout << " " << str2 << endl; Tracker hw{}; cout << hw.get() << endl; { Tracker str3(10u, '+'); cout << str3 << endl; cout << count() << endl; } cout << count() << endl; cout << count() << endl; }