Questions regarding initialization code of static members
Questions tagged [static-initialization]
292 questions
5
votes
1 answer
objective c - is local static variable initialization thread safe?
note: i'm using objective-c++ where non-compile-time constant is allowed (https://stackoverflow.com/a/12304815/3101492)
+ (Foo)sharedFoo
{
static Foo *foo = [Foo new];
return foo;
}
static initializer is expected to be run only once, but…
![](../../users/profiles/3101492.webp)
jason na
- 240
- 1
- 10
5
votes
1 answer
Initialize static class implicitly
is it possible to initialize a static class on app start up "automatically"?
By automatically I mean without the need of referencing a property.
The reason I want to be able to do this for is that I'd like to automatically theme an app on start…
![](../../users/profiles/3620174.webp)
pikausp
- 993
- 9
- 27
4
votes
1 answer
Static Initialization and Use of a Class in a Separate Module in D
In my program, I have a class that I want to be allocated before entering main(). I'd like to tuck these away in a separate module to keep the clutter out of my code; However, as soon as the module goes out of scope (before main() is entered), the…
![](../../users/profiles/615387.webp)
Meta
- 1,061
- 5
- 14
4
votes
2 answers
static member explicit definition
Consider this code:
#include
using namespace std;
class Wilma
{
public:
static int i;
Wilma()
{
cout<<"\nWilma ctor\n";
cout<<"\ni::"<
![](../../users/profiles/30868.webp)
ashishsony
- 2,397
- 2
- 24
- 35
4
votes
3 answers
std::set used as a static templated member variable
I am trying to make something like a Java style Enum, which I'm calling a flag. The requirements are that each flag is static so flags are directly referencable, each flag storing the string of it's name and the whole set iterable and conducive to…
![](../../users/profiles/745247.webp)
user745247
- 43
- 5
4
votes
1 answer
Static inline members initialization order
A well known problem in C++ is the static initialization order fiasco. Is it still considered a problem when one use C++17 static inline members?
Here an example where a static inline member is used in two different translation units (a.cpp and…
![](../../users/profiles/9659683.webp)
Tarquiscani
- 559
- 5
- 18
4
votes
2 answers
c++ string constant and static initialization order fiasco
I'm trying to understand when the static initialization order fiasco is a real problem. If I use a string constant like kName below would it suffer any of the problems of the static initialization order fiasco? Is this a problem in this case because…
![](../../users/profiles/4047104.webp)
Mike Sweeney
- 1,396
- 1
- 13
- 18
4
votes
2 answers
Calling function before main in static libraries
I have a type registration system for a custom form of runtime type information. Up until now I've used the following macro to call a registration function before main and register the types:
#define REGISTRATION …
![](../../users/profiles/906367.webp)
DavidColson
- 7,437
- 9
- 32
- 47
4
votes
2 answers
Why do I get an NPE when a nested Enum references a parent static member in its constructor?
Conditions to recreate (as far as I can tell):
nested enum references a parent static member
nested class
static member of parent class takes enum as an constructor argument to nested class
enum is referenced by an external class before anything…
![](../../users/profiles/9638775.webp)
rymach
- 83
- 7
4
votes
1 answer
Multiple initialization of Static library members when in multiple shared libraries
Note that this is not a duplicate of Multiple instances of singleton across shared libraries on Linux since adding -rdynamic flag doesn't change what is described in the question.
I have a static c++ library which has some static initialization…
![](../../users/profiles/5785912.webp)
Léonard
- 41
- 6
4
votes
0 answers
C++ early dynamic initialization
In the current C++ standard, the paragraph [basic.start.static] p.3 says (emphasis mine):
An implementation is permitted to perform the initialization of a
variable with static or thread storage duration as a static
initialization even if such…
![](../../users/profiles/3766405.webp)
user42768
- 1,791
- 7
- 18
4
votes
1 answer
java static inner class initialization errors
Context:
java.io.File class has a static inner class method as follows:
LazyInitialization.temporaryDirectory();
[EDITED to add some more code]
My code below eventually calls the above line of code. An exception is thrown from within the…
![](../../users/profiles/204312.webp)
crazy horse
- 423
- 2
- 9
- 25
4
votes
1 answer
C++ symbol analysis: how to determine which static initialization is performed?
I want to analyze what causes the huge size of my shared C++ library which is compiled by GCC (v.6.1.1) on Linux.
readelf -sW libfoo.so tells me that are particularly big functions called __static_initialization_and_destruction_0,…
![](../../users/profiles/678093.webp)
m.s.
- 15,040
- 6
- 49
- 79
4
votes
3 answers
Cost of thread-safe local static variable initialization in C++11?
We know that local static variable initialization is thread-safe in C++11, and modern compilers fully support this. (Is local static variable initialization thread-safe in C++11?)
What is the cost of making it thread-safe? I understand that this…
![](../../users/profiles/3562361.webp)
Sunanda
- 85
- 6
4
votes
1 answer
How to comprehend that an implementation is permitted to treat dynamic initialization of non-local variable as static initialization in some cases?
In fact, the problem comes from the words in the standard draft N4582:
[basic.start.static/3] An implementation is permitted to perform the initialization of a variable with static or thread storage duration as a static initialization even if such…
![](../../users/profiles/5376789.webp)
xskxzr
- 10,946
- 11
- 32
- 70