title: Comparing a boost::optional<T&> to const T&
body: <p>I'm trying to compare a constant reference and a non-constant optional object with the same type.  I have a type, <code>NonCopy</code>, which is noncopyable</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;boost/optional.hpp&gt;
  
  struct NonCopy {
      NonCopy() { }
      NonCopy(const NonCopy&amp;) = delete;
      NonCopy&amp; operator=(const NonCopy&amp;) = delete;
  };
  
  int main()
  {
      NonCopy nc;
      const NonCopy&amp; object = nc;
      boost::optional&lt;NonCopy&amp;&gt; object2 = nc;
  
      if (!object2 &amp;&amp; object2.get() != object) {
          std::cout &lt;&lt; "not equal?\n";
      }
  }
  </code></pre>
  
  <p>Which yields</p>
  
  <pre><code>error: no match for ‘operator!=’ (operand types are ‘boost::optional_detail::types_when_is_ref&lt;NonCopy&amp;&gt;::raw_type {aka NonCopy}’ and ‘const NonCopy’)
  </code></pre>
  
  <p>I've tried multiple variations on the theme, including</p>
  
  <pre><code>if (object2 &amp;&amp; const_cast&lt;const NonCopy&amp;&gt;(object2.get()) != object)
  </code></pre>
  
  <p>Which yields a very interesting error of</p>
  
  <pre><code>error: no match for ‘operator!=’ (operand types are ‘const NonCopy’ and ‘const NonCopy’)
  </code></pre>
  
  <p>and lists candidates for != on <code>boost::optional&lt;NonCopy&gt;</code> (such as <code>bool boost::operator!=(const boost::optional&lt;NonCopy&gt;&amp;, const boost::optional&lt;NonCopy&gt;&amp;)</code>), rather than on <code>NonCopy</code>.</p>