title: Why does a lock_guard on a mutex reference produce C26110
body: <p>The following code in a Visual Studio Professional 2019 project (version 16.3.6) produces a warning: </p>
  
  <pre><code>#include &lt;thread&gt;
  #include &lt;future&gt;
  
  class Foo {
  public:
      mutable std::recursive_mutex _writingMutex;
      std::recursive_mutex&amp; writingMutex() const { return _writingMutex; }
  };
  
  int main()
  {
      Foo a;
      std::lock_guard&lt;std::recursive_mutex&gt; lock(a.writingMutex()); // produces C26110
      std::lock_guard&lt;std::recursive_mutex&gt; lock2(a._writingMutex); // no warning
  }
  </code></pre>
  
  <p>The first lock produces the warning <a href="https://docs.microsoft.com/en-us/visualstudio/code-quality/c26110?view=vs-2019" rel="noreferrer">C26110</a>:</p>
  
  <blockquote>
    <p>Warning   C26110  Caller failing to hold lock 'lock' before calling function 'std::lock_guard::~lock_guard'</p>
  </blockquote>
  
  <p>Why is this so? Does passing the mutex as reference not work?</p>