2

I have seen it written that:

The static initializer is executed before the first invocation of the containing function; the initializer expression must be a compile-time constant.

Consider this:

void func(){
   static float tr=((float) rand() / (RAND_MAX));
}

tr depends on a runtime function, rand(). I don't think the value of rand() is known at compile time, is it? Yet this compiles fine in C++ and a lot of answers/literature indicate that C behavior is the same as C++ in this regard.

Community
  • 1
  • 1
johnbakers
  • 24,158
  • 24
  • 130
  • 258
  • C or C++? They are different languages. – Sourav Ghosh Feb 07 '16 at 08:41
  • @SouravGhosh on this issue, everything I have read (including the linked answer) says that the C and C++ behaviour for a static variable in a function is the same. – johnbakers Feb 07 '16 at 08:47
  • Just a random guess: can it be due to a strict/pedantic/so on flag not being toggled in the compiler? – iksemyonov Feb 07 '16 at 08:50
  • @johnbakers : That is certainly not true. This can clearly be seen by compiling your example as C and C++. – Clifford Feb 07 '16 at 08:52
  • 1
    @SouravGhosh : It is legitimate to ask a question about more than one language. Especially when they have a common subset and common tools - then the differences between them become important to understand and it is useful to discuss them together. However for this to compile, her is clearly using C++ because it is not valid C. – Clifford Feb 07 '16 at 08:56
  • 1
    So either take out the "Yet this compiles fine." and replace it with something that correctly indicates the difference between C and C++, or take out the [tag:c] tag. Right now, this question is spreading false information. –  Feb 07 '16 at 09:01
  • @hvd done, thanks. indeed it was false information, learned from other false information elsewhere. – johnbakers Feb 07 '16 at 09:02

2 Answers2

6

In C++ a local static initialization is performed the first time the scope is entered and the expression doesn't need to be a constant at all. You can call any function you like. A common patter for singletons is for example:

MySingleton& get_instance() {
    static MySingleton s;
    return s;
}

where the instance will be constructed only when (and if) the get_instance function is called. With C++11 you're even guaranteed that there will be no problem if get_instance is possibly called from multiple threads at the same time as the compiler will add the needed locking logic.

In C things are different and static initialization is performed by the loader before program starts and you can only use constant expressions so the code in your question is not valid (you cannot call rand).

6502
  • 112,025
  • 15
  • 165
  • 265
  • I had to repeat myself at least 3 times this: `In C++ a local static initialization is performed the first time the scope is entered ` Nice and clear explanation, BTW – Alex Vergara Dec 29 '21 at 13:00
3

It is valid C++, it is not valid C.

The answer linked in the question from where the quote comes from refers to the behaviour of C, but the question is clearly specific to C++.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Good to know. So then what *is* the expected behavior? The static variable depends on runtime, but is guaranteed to get initialized not at compile time, but sometime before the function is first invoked? – johnbakers Feb 07 '16 at 08:55