Linked Questions

251 votes
2 answers
100k views

I know this is an often asked question, but as there are so many variants, I'd like to re-state it, and hopefully have an answer reflecting the current state. Something like Logger& g_logger() { ...
Ralph Zhang's user avatar
  • 5,443
59 votes
4 answers
47k views

I noticed that if you initialize a static variable in C++ in code, the initialization only runs the first time you run the function. That is cool, but how is that implemented? Does it translate to ...
bobobobo's user avatar
  • 67.9k
20 votes
8 answers
7k views

This question made me question a practice I had been following for years. For thread-safe initialization of function-local static const objects I protect the actual construction of the object, but ...
sbi's user avatar
  • 225k
7 votes
6 answers
10k views

The following small example implements a singleton pattern that I've seen many times: #include <iostream> class SingletonTest { private: SingletonTest() {} static SingletonTest *instance; ...
sje397's user avatar
  • 41.9k
4 votes
6 answers
3k views

I am porting a class to C++ and need to execute some initialization code before the first instance of my class is created; executing the code before main() gets control suits me. How to do it in C++?
kludg's user avatar
  • 27.6k
6 votes
4 answers
2k views

I have the following sample C++ code: class Factory { public: static Factory& createInstance() { static Factory fac; return fac; } private: Factory() { ...
Naveen's user avatar
  • 73.8k
9 votes
5 answers
210 views

I've typically gotten used to implementing a singleton pattern in this manner because it's so easy: class MyClass { public: MyClass* GetInstance() { static MyClass ...
John Humphreys's user avatar
4 votes
4 answers
3k views

I have a method / function: void foo() { static const std::string strSQLQuery = "SELECT ... "; // or maybe const std::string strSQLQuery = "SELECT ... "; // some operations on strSQLQuery ...
jacek.ciach's user avatar
  • 1,062
6 votes
4 answers
761 views

Consider the following scenario. We have a C++ function with a static local variable: void function() { static int variable = obtain(); //blahblablah } the function needs to be called from ...
sharptooth's user avatar
  • 171k
4 votes
2 answers
1k views

In paper http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2660.htm an algorithm is presented that does not need to hold a lock during the initialization of a local static variable but still ...
Johannes Schaub - litb's user avatar
8 votes
1 answer
2k views

I wrote the following toy example: std::map<char, size_t> getMap(const std::string& s) { std::map<char, size_t> map; size_t i = 0; for (const char * b = s.data(), *end = b ...
Arnaud's user avatar
  • 3,761
1 vote
2 answers
441 views

I'm following this singleton pattern, why error LNK2001: unresolved external symbol in this case? my problem LOOKs similar, but my issue is not with the definition of the static instance. My problem ...
FlavorScape's user avatar
  • 14.5k
0 votes
3 answers
627 views

So I want to access a singleton class from multiple threads. Conceptually I'd think that calling non-const methods on this singleton instance would be not thread-safe. I've been looking online and no ...
Reggie's user avatar
  • 73
-1 votes
1 answer
489 views

I have a simple function to handle the parsing and hashing of the Sec-WebSocket-Key value in a C websocket program. I had the whole program working but found out I had a bunch of char* without a ...
mando222's user avatar
  • 543
0 votes
2 answers
234 views

In C++, static variables defined inside a function are thread safe by default. So, are static variables in Rust have the same thread safety property or I need to use some sync primitive?
Harry's user avatar
  • 4,144