下面的代码应该初始化一个static thread-local和static结构。
#include <iostream>
struct Tracer {
public:
Tracer(const char *new_name) : name{new_name} {
printf("%s : Constructor()\n", this->name);
}
~Tracer() {
printf("%s : Destructor()\n", this->name);
}
private:
const char *name;
};
// 1. Thread-Local Storage Duration
static thread_local Tracer t_thread_local{"Thread-Local Storage Duration"};
// 2. Static Storage Duration
static Tracer t_static{"Static Storage Duration"};
int main() {
printf("Start Program\n");
}
Run Code Online (Sandbox Code Playgroud)
但是,我没有看到static thread-localstruct Constructor/Destructor预期的消息。打印的输出仅显示来自static结构的消息。我错过了什么吗?
Static Storage Duration : Constructor()
Start Program
Static Storage Duration : …Run Code Online (Sandbox Code Playgroud) c++ ×1