Equivalent of C++ function-scope static initialization

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Mar 2 15:07:30 PST 2015


On 03/02/2015 02:06 PM, Mark Isaacson wrote:

 > I'm looking for the D equivalent of:
 >
 > //C++
 > void foo() {
 >    static string bar = painfulToInitialize(); //Always returns the same
 > value
 >    /* A bunch of code */
 > }

immutable string bar;

shared static this()
{
     bar = painfulToInitialize();
}

void foo() {
}

When needed, 'bar' can be mutable as well but you have to take care of 
synchronization yourself in that case.

 > I don't need the thread-safety that C++ provides in that case,

I am not aware of such safety. (?) Is that a newer C++ feature?

Ali



More information about the Digitalmars-d-learn mailing list