Static variable in h-file C++ template library. Is it OK? [duplicate] - c++

This question already has answers here:
How to have static data members in a header-only library?
(3 answers)
Closed 6 years ago.
I am developing lightweight parser as C++ h-file template library.
Gramma is described in specific BNF-like notation using overloaded operators on some classes which should be enumerated somehow. I need just one global variable as some counter performing it.
I do not want to use extern int var; in h-file and int var; in cpp-file because all my stuff in single header file and now the user just needs to include it.
I can declare static int var; in header file but copy of this variable appears in all object files where my header file is included.
Is it OK for template library?
Any suggestions?

As already mentioned you can use singleton pattern.
This version doesn't require definition of static member in template cpp file.
template <typename T> class Tmpl
{
public:
static Tmpl<T>& GlobalInstance()
{
static Tmpl<T> m_Singleton;
return m_Singleton;
};
};

Related

How to implement static class members in a header-only library? [duplicate]

This question already has answers here:
C++: Header-only project, static const non-integral
(1 answer)
How can you define const static std::string in header file?
(2 answers)
C++ static const string in header c++11
(2 answers)
Closed 2 years ago.
I am implementing a new header-only Library in VS2019, targeting C++11. As discussed (How to initialize a static const member in C++?) you cannot initialize a non-int static class member in-line.
class A {
private:
static const std::string SPECIAL_KEY = "A8787HHH"; //NOT OK
/*...*/
};
Instead you have to define it outside the class, typically in a CPP file:
class A {
private:
static const std::string SPECIAL_KEY;
/*...*/
};
const std::string A::SPECIAL_KEY = "A8787HHH";
But in a header-only library this runs into problems. I tried to do the above all in the same header and it worked in a simple test-app, but in real use I got linker errors that symbol A::SPECIAL_KEY was multiply defined, not surprising really as the header will get included in multiple places (How to initialize private static members in C++?)
In C++17 you can define in-line (https://stackoverflow.com/a/45062055/197229) but that's not any help to me on this code-base. C++ doesn't provide static class constructors either.
Header-only libraries have become quite widely used so is there a standard workaround to what must be a common sort of problem, until such time as we might be able to upgrade to C++17?

What are the possible advantages of using the "static" directive outside of a class? [duplicate]

This question already has answers here:
The static keyword and its various uses in C++
(9 answers)
Closed 8 years ago.
I've been doing some experimenting with function libraries and namespaces. I noticed that you can declare a function in a namespace as static, although it is not within a class declaration:
hpp:
ANameSpace
{
static void aFunc();
};
cpp:
ANameSpace
{
static void aFunc()
{
std::cout<<"Static function called"<<std::endl;
}
};
I understand the concept of static class members and how they can be very useful, but are there any particular advantages to using static outside a class?
Note: I cast the final vote to close this question (can't delete because there is an answer) because the reference cited as dup, although it is much broader in scope, contains a detailed discussion of static. But I'm not sure if it exactly answers the question as I have rephrased it now.
A static global function is visible only in the scope of the file. This is probably due to C compatibility, where this was also possible.
This sums up every possible use of static pretty well.

setting static member variable inside a static method [duplicate]

This question already has answers here:
Undefined reference to static variable [duplicate]
(2 answers)
Closed 9 years ago.
I am beginner to C++ and have a doubt about static member variables and member functions.
I have implemented a class as follows -
class Foo
{
private:
static int myVariable;
public:
static void setMyVariable()
{
myVariable = 100;
}
static void resetMyVariable()
{
myVariable = 0;
}
};
There are following considerations when I wrote a code like that -
I want only one instance of class Foo. Thats why I made all member variables and functions as static.
I don't want the outside code to touch myVariable
I have put this class in a header file and included in my main file. When I do this, I get an error undefined reference to Foo::myVariable
I want to know if I can write a code which can satisfy above requirements?
Thanks !
You need to define static class variables somewhere:
e.g. in your main C++ file,
int Foo::myVariable;
Note that technically, by making everything static, you may have no instances of Foo.

Static vector initialization in cpp file [duplicate]

This question already has answers here:
How to initialize private static members in C++?
(18 answers)
Closed 8 years ago.
I have a problem where I have declared a static string vector in the .h file inside a class ,
.h file
static std::vector<std::string> VHDSigBuffer;
How to use this vector in my class function Implementations in cpp file?
In my_class.h header
class my_class
{
public:
// Declaration
static std::vector<std::string> VHDSigBuffer;
};
In my_class.cpp implementation
// Definition
std::vector<std::string> my_class::VHDSigBuffer;
Now you can freely use my_class::VHDSigBuffer.
You need to instantiate it within your CPP file, with a line like this:
std::vector<std::string> MyClass::VHDSigBuffer;
This line will be outside any function definition in the CPP file.
If I understand what you're saying correctly,
If you want to use this vector in its own class you can access it as VHDSigBuffer, or if you want to use it in another class you can access it as youClass::VHDSigBuffer.

Why does c++ class need to define static field(data member) outside the class scope? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Initializing private static members
Why I can't initialize non-const static member or static array in class?
It is strange to me. Why not assume there is a static field at the global scope?
It has to be placed somewhere (in some object file), so linker could find it. If you have declaration of class with static filed in .h file and include this file in a few .cpp files, then it would be ambiguous, which object file should have place allocated for this filed.
Please also note, that primitive type const static field could be initialized in class declaration:
class Foo
{
static const int n = 42;
};