You can still define a member of the template and explicitly create it for all the required specializations. If you insist on having a state data element of a class template that roughly matches.
Given that global variables share all kinds of problems, including dependency problems during initialization, you are much better off wrapping your data member with a static member function:
template <typenane T> class foo {
A local variable is initialized the first time the function is called, and it can be safely used. This approach also works for templates.
Please note that I still recommend avoiding global variables! They cause many problems in the long-term and short-term benefits of their use - this is a huge debt that usually does not pay off.
source share