views:

42

answers:

1

Using g++ to declare function-static thread-local storage:

void f() {
    static __thread somePodStruct thing;
    ...
}

can I assume that thing will get zero-initialized?

+1  A: 

According to the GCC documentation:

http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Thread-Local.html

In C++, if an initializer is present for a thread-local variable, it must be a constant-expression, as defined in 5.19.2 of the ANSI/ISO C++ standard.

So you can explicitly set it to zero.
So to be on the safe side with no down side of any assumptions you can get zero initialization be explicitly doing it yourself.

Martin York
It's exactly that documentation that raises the question, since it doesn't go on to say what should happen if there isn't an initializer. If the rules are otherwise the same as for static variables, then I should be able to assume zero-initialization, but I can't see that spelled out anywhere.
Bob Lied
Unless it is spelled out explicitly then no you can't assume it. But when there is a perfectly reasonable alternative of adding two characters '=0' that achieves the same affect then why would you even assume that something worked; just do it explicitly.
Martin York