The function calls are not optimized out, they are reordered. It is compiled as if:
_sleep(10);
start = timestamp();
elapsed = timestamp()-start;
cout << name<<(int) elapsed << endl;
This reordering is legal as neither _sleep nor timestamp constitute IO. You can prevent such reordering with a signal handler memory fence, if you have c++11 support. Just include and insert:
std::atomic_signal_fence(std::memory_order_seq_cst);
at the beginning of the destructor and constructor bodies. With GCC 4.7 and 4.8 on x86_&4, such a memory fence will not produce any code, just constrain the compilers reordering.