title: Did my compiler ignore my unused static thread_local class member?
body: <p>I want do some thread registration in my class, so I decide to add a check for the the <code>thread_local</code> feature:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  
  class Foo {
   public:
    Foo() {
      std::cout &lt;&lt; "Foo()" &lt;&lt; std::endl;
    }
    ~Foo() {
      std::cout &lt;&lt; "~Foo()" &lt;&lt; std::endl;
    }
  };
  
  class Bar {
   public:
    Bar() {
      std::cout &lt;&lt; "Bar()" &lt;&lt; std::endl;
      //foo;
    }
    ~Bar() {
      std::cout &lt;&lt; "~Bar()" &lt;&lt; std::endl;
    }
   private:
    static thread_local Foo foo;
  };
  
  thread_local Foo Bar::foo;
  
  void worker() {
    {
      std::cout &lt;&lt; "enter block" &lt;&lt; std::endl;
      Bar bar1;
      Bar bar2;
      std::cout &lt;&lt; "exit block" &lt;&lt; std::endl;
    }
  }
  
  int main() {
    std::thread t1(worker);
    std::thread t2(worker);
    t1.join();
    t2.join();
    std::cout &lt;&lt; "thread died" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>The code is simple.  My <code>Bar</code> class has a static <code>thread_local</code> member <code>foo</code>. If a static <code>thread_local</code> <code>Foo foo</code> is created, it means a thread is created.</p>
  
  <p>But when I run the code, nothing in the <code>Foo()</code> prints, and if I remove the comment in <code>Bar</code>'s constructor, which uses <code>foo</code>, the code works fine.</p>
  
  <p>I tried this on GCC (7.4.0) and Clang (6.0.0) and the results are the same.
  I guess that the compiler discovered that <code>foo</code> is unused and do not generate an instance. So</p>
  
  <ol>
  <li>Did the compiler ignore the <code>static thread_local</code> member? How can I
  debug for this?</li>
  <li>If so, why does a normal <code>static</code> member not have this problem?</li>
  </ol>