4 ms·
I learned a lot from this, neat article! One tip about initializing global variables in D -- the common idiom is to use a "shared static this() {}" block. This
by gavinray 3y ago
I learned a lot from this, neat article!
One tip about initializing global variables in D -- the common idiom is to use a "shared static this() {}" block. This lets you set immutable variables once at the start of the program.
Since this requires the D Runtime, you can't use it in betterC, but you can use a pragma to mark a "crt_constructor" function as an equivalent:
pragma(crt_constructor)
extern(C) void init_global_state() {
// Set BSS pointers up here
}
I'm not sure whether this works in bare-metal environments (and maybe that's why it's not used).
- CyberDildonics 3y agoWhy does it need the D runtime to set a variable once? This seems very complicated.
- gavinray 3y agoTo be honest, I'm not entirely sure of the technical reason: https://forum.dlang.org/post/sgiyhdnpdyxvigwtrfqk@forum.dlang.org https://forum.dlang.org/post/sgiyhdnpdyxvigwtrfqk@forum.dlan...
- adrian_b 3y agoI suppose that this is needed if you want to guarantee that all the constructors that are executed before main() are executed in a certain order. I have not searched the latest versions of the C++ standard to see if anything has changed, but in the older versions of C++ it was impossible to ensure that the global constructors will be executed in a certain order (to be able to satisfy dependencies between them). For static variables that only have initial values taken from the executable file, without initialization code, I assume that something like this is not necessary.
- pjmlp 3y agoStill the same in regards to C++.