Re: Classes for enums
On Apr 8, 5:32 am, Jerry Coffin <jcof...@taeus.com> wrote:
In article <be639515-1ce5-4b70-97a6-
fecccee77...@r28g2000vbp.googlegroups.com>, vlazare...@volanttrading.com
says...
On Apr 7, 2:40 pm, Jerry Coffin <jcof...@taeus.com> wrote:
std::ostream &operator<<(std::ostream &out, value_type v) {
static char const *names[] = {
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday"
};
if (v >= count)
throw std::logic_error("bad day");
return out << names[v];
}
[ ... ]
Is that initialization of static array inside operator <<
thread-safe?
The current standard doesn't really provide an answer (to
anything about threading), but generally speaking the answer
is yes anyway. The part that would be tricky would be if it
was invoked (indirectly?) from the ctor of a static object, so
the code executed before main was invoked -- but with or
without threads, you generally need to do some non-standard
things for streams to work under such circumstances, so this
doesn't really change much (if anything).
I'm not sure I see the order of initialization issue for a local
variable. The standard doesn't (currently) say anything about
threading, but it does distinguish between static and dynamic
initialization. In a way that means that in practice, static
initialization (as above) doesn't cause any instructions to be
executed, and thus must be thread neutral. (In practice, this
local variable will be initialized before any code, even that in
the constructors of static objects, is executed, by the OS
copying its image from the executable file into memory.)
Of course, if you used an std::vector< std::string > instead of
the C types, the issues would be different.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34