title: How does scoped_lock avoid emitting an "unused variable" warning?
body: <p><code>boost::mutex::scoped_lock</code> is a handy RAII wrapper around locking a mutex. I use a similar technique for something else: a RAII wrapper around asking a data interface to detach from/re-attach to a serial device.</p>
  
  <p>What I can't figure out, though, is why in the code below only my object <code>mst</code> &mdash; whose instantiation and destruction <em>do have side effects</em> &mdash; causes <code>g++</code> to emit an "unused variable" warning error whereas <code>l</code> manages to remain silent.</p>
  
  <p>Do you know? Can you tell me?</p>
  
  <pre><code>[generic@sentinel ~]$ cat test.cpp
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/thread/mutex.hpp&gt;
  #include &lt;iostream&gt;
  
  struct MyScopedThing;
  struct MyWorkerObject {
      void a() { std::cout &lt;&lt; "a"; }
      void b() { std::cout &lt;&lt; "b"; }
  
      boost::shared_ptr&lt;MyScopedThing&gt; getScopedThing();
  };
  
  struct MyScopedThing {
      MyScopedThing(MyWorkerObject&amp; w) : w(w) {
          w.a();
      }
      ~MyScopedThing() {
          w.b();
      }
  
      MyWorkerObject&amp; w;
  };
  
  boost::shared_ptr&lt;MyScopedThing&gt; MyWorkerObject::getScopedThing() {
      return boost::shared_ptr&lt;MyScopedThing&gt;(new MyScopedThing(*this));
  }
  
  int main() {
      boost::mutex m;
      boost::mutex::scoped_lock l(m);
  
      MyWorkerObject w;
      const boost::shared_ptr&lt;MyScopedThing&gt;&amp; mst = w.getScopedThing();
  }
  
  
  [generic@sentinel ~]$ g++ test.cpp -o test -lboost_thread -Wall
  test.cpp: In function ‘int main()’:
  test.cpp:33: warning: unused variable ‘mst’
  
  [generic@sentinel ~]$ ./test
  ab[generic@sentinel ~]$ g++ -v 2&gt;&amp;1 | grep version
  gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC)
  </code></pre>