1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <atomic>
#include <iostream>
#include <functional>
namespace TestLogCtor
{
//
//----------------------------------------------------------------------------
//CountAndLog class
class CountAndLog
{
static std::atomic<int> idGenerator;
public:
const int id;
CountAndLog() : id(idGenerator.fetch_add(1)) { Log("ctor"); }
CountAndLog(CountAndLog const &src) : id(idGenerator.fetch_add(1)) { Log("cctor", src.id); }
CountAndLog(CountAndLog &&src) : id(idGenerator.fetch_add(1)) { Log("mctor", src.id); }
~CountAndLog() { Log("dtor"); }
CountAndLog& operator=(CountAndLog const &src) { Log("op=", src.id); }
CountAndLog& operator=(CountAndLog &&src) { Log("mop=", src.id); }
private:
void Log(const char *text);
void Log(const char *text, int idFrom);
};
std::atomic<int> CountAndLog::idGenerator;
void CountAndLog::Log(const char *text)
{
std::cout << "CountAndLog #" << id << ": " << text << std::endl;
}
void CountAndLog::Log(const char *text, int idFrom)
{
std::cout << "CountAndLog #" << id << ": " << text << " from #" << idFrom << std::endl;
}
//
//----------------------------------------------------------------------------
//Rest of the test
using fun = std::function<void(int)>;
class SomeClass : private CountAndLog
{
public:
void operator()(int x_)
{
std::cout << "x " << x_ << std::endl;
}
};
void show(const fun& f_)
{
f_(1);
}
void test_function2()
{
fun fff;
{
SomeClass ccc;
fff = ccc;
}
show(fff);
}
}//namespace TestLogCtor |
Partager