Equivalent of C++ function-scope static initialization

Mark Isaacson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Mar 2 17:17:13 PST 2015


On Monday, 2 March 2015 at 23:07:30 UTC, Ali Çehreli wrote:
> immutable string bar;
>
> shared static this()
> {
>     bar = painfulToInitialize();
> }
>
> void foo() {
> }

Clever :).


> > 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

New as of C++11. All function-scope statics are guaranteed to be 
initialized in a thread-safe manner. Exactly one thread will 
initialize the variable the first time the function is called.


More information about the Digitalmars-d-learn mailing list