CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
static_constructor.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_UTILITY_STATIC_CONSTRUCTOR_H
10#define CPPCOMMON_UTILITY_STATIC_CONSTRUCTOR_H
11
12namespace CppCommon {
13
14#if defined(__GNUC__)
15#pragma GCC diagnostic push
16#pragma GCC diagnostic ignored "-Waddress" // GCC: warning: the address of 'expression', will always evaluate as 'true'
17#elif defined(_MSC_VER)
18#pragma warning(push)
19#pragma warning(disable: 4127) // C4127: conditional expression is constant
20#endif
21
23
47template <void (*construct)(), void (*destruct)() = nullptr>
49{
50 struct constructor;
51
52public:
54 static constructor instance;
55
56private:
57 struct constructor
58 {
59 constructor()
60 {
61 if (construct != nullptr)
62 construct();
63 }
64
65 ~constructor()
66 {
67 if (destruct != nullptr)
68 destruct();
69 }
70
71 void operator()() {}
72 };
73};
74
75#if defined(__GNUC__)
76#pragma GCC diagnostic pop
77#elif defined(_MSC_VER)
78#pragma warning(pop)
79#endif
80
83} // namespace CppCommon
84
86
87#endif // CPPCOMMON_UTILITY_STATIC_CONSTRUCTOR_H
Static constructor pattern.
static constructor instance
Static constructor instance.
C++ Common project definitions.
Static constructor pattern inline implementation.