mettle
mettle copied to clipboard
Work around Clang/Windows init order bug by using static all_suites
This change replaces the inline definition of all_suites with a static one to avoid static initialization order issues, particularly on Clang + Windows.
This ensures all_suites is initialized before any static test registrations, in accordance with the C++ standard, which guarantees initialization order for static variables within a single translation unit.
However, this imposes a limit on test executable to have only one translation unit. Not sure how to do it better without explicit initialization point and slightly more boilerplate, which this patch avoids for now.
Fixes #52
While I'm fairly sure this is a compiler bug, if we did work around this, I think the right way would be to just revert the relevant part of 7a9adee3 and use the old implementation:
inline suites_list & all_suites() {
static suites_list all;
return all;
}
I think it makes sense to revert it then, otherwise it's not usable on Clang/Windows. Shall I update the MR?
As mentioned in #52, I think there's a good chance this is a Clang bug. I'll try to make a minimal reproducer and file this upstream. If it turns out that I'm wrong and Clang's behavior on Windows is standards-compliant, then I'll (partially) revert 7a9adee30b095e49c4b6a533a67ff607eb4c5f99.