static variables for non-constant expressions?

Simon s.d.hammett at gmail.com
Mon Apr 11 15:06:25 PDT 2011


On 11/04/2011 22:15, Stewart Gordon wrote:
> On 11/04/2011 02:37, Jonathan M Davis wrote:
> <snip>
>>> I don't know the background of how static variables really work, so is
>>> there a good reason why the first function can't work like the one below
>>> it?
>>
>> They have to be calculated at compile time so that ordering doesn't
>> matter. If
>> the order mattered, then you get into dependency problems or risk using
>> undefined variables. Languages like C++ and Java have problems with that.
> <snip>
>
> I recall reading that in C++, static variables are initialised on first
> call. Which would have to mean that it does something like that internally.
>
> But I might be imagining it. I'll have to experiment.
>
> Can you give an example of the dependency problems this might lead to?
>
> Stewart.

In visual C++ all static vars with assignments/constructors get 
initialised by the CRT before main starts, though in an undefined order. 
Can lead to bugs if you use them carelessly.

Pretty sure that applies to other compilers as well.

-- 
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk


More information about the Digitalmars-d-learn mailing list