@sipa, see the comments in sync and logging. Basically the order in what you need the destructor to be called might be different from the inverse order they were constructed.
See a simple example that segfaults if I dropped that requirement:
(The ~Init destructor uses the log after the logger has been destructed)
#include <iostream>
#include <memory>
struct Log{
std::unique_ptr<const std::string> m_prefix;
Log(){m_prefix=std::unique_ptr<const std::string>(new std::string{"::: "});log(__func__);};
~Log(){log(__func__);};
void log(const std::string&s){std::cout << *m_prefix << s << std::endl;}
};
Log& LogInstance() {
static Log l;
return l;
}
struct Init{
Init(){std::cout << __func__<<std::endl;}
~Init(){LogInstance().log(__func__);}
} g_init;
int main()
{
LogInstance().log(__func__);
}