您可以使用与标准流std::cout 及其朋友相同的方法。它被称为Schwarz Counter or Nifty Counter。
如果您查看 GNU libstdc++ 的 ios_base.h 标头:
// 27.4.2.1.6 Class ios_base::Init
// Used to initialize standard streams. In theory, g++ could use
// -finit-priority to order this stuff correctly without going
// through these machinations.
class Init
{
friend class ios_base;
public:
Init();
~Init();
private:
static _Atomic_word _S_refcount;
static bool _S_synced_with_stdio;
};
并进入iostream 标头:
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
extern istream cin; /// Linked to standard input
extern ostream cout; /// Linked to standard output
extern ostream cerr; /// Linked to standard error (unbuffered)
extern ostream clog; /// Linked to standard error (buffered)
// For construction of filebuffers for cout, cin, cerr, clog et. al.
static ios_base::Init __ioinit;
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace
进入ios_init.cc:
ios_base::Init::Init()
{
if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0)
{
// Standard streams default to synced with "C" operations.
_S_synced_with_stdio = true;
new (&buf_cout_sync) stdio_sync_filebuf<char>(stdout);
new (&buf_cin_sync) stdio_sync_filebuf<char>(stdin);
new (&buf_cerr_sync) stdio_sync_filebuf<char>(stderr);
// The standard streams are constructed once only and never
// destroyed.
new (&cout) ostream(&buf_cout_sync);
new (&cin) istream(&buf_cin_sync);
new (&cerr) ostream(&buf_cerr_sync);
new (&clog) ostream(&buf_cerr_sync);
cin.tie(&cout);
cerr.setf(ios_base::unitbuf);
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 455. cerr::tie() and wcerr::tie() are overspecified.
cerr.tie(&cout);
// NB: Have to set refcount above one, so that standard
// streams are not re-initialized with uses of ios_base::Init
// besides <iostream> static object, ie just using <ios> with
// ios_base::Init objects.
__gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1);
}
}
ios_base::Init::~Init()
{
// Be race-detector-friendly. For more info see bits/c++config.
_GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_S_refcount);
if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, -1) == 2)
{
_GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_S_refcount);
// Catch any exceptions thrown by basic_ostream::flush()
__try
{
// Flush standard output streams as required by 27.4.2.1.6
cout.flush();
cerr.flush();
clog.flush();
}
__catch(...)
{ }
}
}
上面将具有静态存储持续时间的全局对象__ioinit嵌入到每个包含iostream头文件的翻译单元(.o)中。 IE。每个.o 都有自己的__ioinit 副本。
所有具有静态存储持续时间的基本类型的对象在静态初始化阶段启动时初始化为零(在 Linux 上,这是一个 elf 对象的 .bss 部分),因此在动态初始化之前将 _S_refcount 分配为 0阶段。
接下来,在动态初始化阶段,这些__ioinit 对象的构造函数被调用。每个构造函数递增_S_refcount 并且观察到_S_refcount 的0 值的__ioinit 对象驻留在首先被初始化的翻译单元中。该对象的构造函数初始化标准流。
C++标准库缺陷报告列表Issue 369: io stream objects and static ctors中有更多信息。
您可以使用相同的方法来初始化您自己的全局对象。例如:
// DynamicInitializer.h
template<class T>
struct DynamicInitializer
{
// These members have to be POD types to be zero-initialized at static initialization phase
// prior to the dynamic initialization phase which invokes constructors of global objects.
static T* instance_;
static unsigned ref_count_;
DynamicInitializer() {
if(!ref_count_++)
instance_ = new T;
}
~DynamicInitializer() {
if(!--ref_count_)
delete instance_;
}
operator T&() const { return *instance_; }
T* operator->() const { return instance_; }
DynamicInitializer(DynamicInitializer const&) = delete;
DynamicInitializer& operator=(DynamicInitializer const&) = delete;
};
template<class T>
unsigned DynamicInitializer<T>::ref_count_ = 0;
template<class T>
T* DynamicInitializer<T>::instance_ = 0;
用法:
// MyLogger.h
struct MyLogger
{
void log(char const*);
};
// const makes static storage.
DynamicInitializer<MyLogger> const my_global_logger;
现在,只要包含MyLogger.h,my_global_logger 就保证在首次使用之前被初始化,例如my_global_logger->log("hello");