colorize_index has different values in different translation units
ZbigniewRA opened this issue · 1 comments
Each translation unit contains it's own copy of the colorize_index
static variable.
This is a normal property of static variables declared on a namespace level (which is a very different thing than static variables declared in a class or inside a function).
As a result colorize_index
is initialized multiple times, and therefore each translation unit gets it's own, different colorize_index
value.
Everything works as long as code that uses it is in one translation unit. Everything breaks if code is separated between translation units,
In my case there was no problem in debug builds, but in optimized builds (on g++/Linux and Visual C++/Windows), where code is heavily inlined, problems started to appear.
One solution to that problem could be storing colorize_index
as a static variable inside a templated class:
template<typename T>
struct ColorizeIndexWrapper
{
static int colorize_index;
};
template<typename T>
int ColorizeIndexWrapper<T>::colorize_index = std::ios_base::xalloc();
// Usage:
_internal::ColorizeIndexWrapper<int>::colorize_index
Hey @ZbigniewRA,
Thanks for reporting this! I wonder, what have I been thinking when I was creating static global variables in the header file. :)
I can easily reproduce this issue with any optimization level on GCC 10.2.0, and -O2
on Clang 11.0.1. I'll work on the patch.