https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103629
--- Comment #14 from Jonathan Wakely <redi at gcc dot gnu.org> ---
So one copy of that variable gets initialized, and a different copy gets
dereferenced, so it dereferences null.
If you change Tree.h to:
#pragma once
#include <mutex>
#include <memory>
#include <cstdio>
class Tree
{
public:
static const std::string treeType() {
static std::once_flag once;
std::call_once(once, []() {
std::printf("%p\n", &sTreeTypeName);
sTreeTypeName.reset(new std::string());
});
std::printf("%p\n", &sTreeTypeName);
if (!sTreeTypeName)
throw "uhoh";
return *sTreeTypeName;
}
private:
static std::unique_ptr<const std::string> sTreeTypeName;
};
std::unique_ptr<const std::string> Tree::sTreeTypeName;
The program prints:
0x4040b0
0x7f81066af0a8
terminate called after throwing an instance of 'char const*'
Aborted (core dumped)