title: Is there a way to detect a classes constness inside of itself?
body: <p>Consider the following example and output:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;type_traits&gt;
  #include &lt;iostream&gt;
  
  struct X {
  
  bool is_const = std::is_const_v&lt;std::remove_pointer_t&lt;decltype(this)&gt;&gt;;
  
  constexpr bool isConst1() const {
      return is_const;
  }
  
  constexpr bool isConst2() const {
      return std::is_const_v&lt;std::remove_pointer_t&lt;decltype(this)&gt;&gt;;
  } 
  
  };
  
  int main() {
      X a;
      const X b;
      std::cout &lt;&lt; "X is const (1): " &lt;&lt; a.isConst1() &lt;&lt; '\n';
      std::cout &lt;&lt; "X is const (2): " &lt;&lt; a.isConst2() &lt;&lt; '\n';
      std::cout &lt;&lt; "const X is const (1): " &lt;&lt; b.isConst1() &lt;&lt; '\n';
      std::cout &lt;&lt; "const X is const (2): " &lt;&lt; b.isConst2() &lt;&lt; '\n';
      return 0;
  }
  </code></pre>
  
  <pre><code>X is const (1): 0
  X is const (2): 1
  const X is const (1): 0
  const X is const (2): 1
  </code></pre>
  
  <p>Apparently, regardless of whether a type is actually const or not, the constness of <code>this</code> always depends on the compile-time context and is not determined at runtime or implicitly templated.</p>
  
  <p>Is there some other way to detect the constness of class from inside it? If so, this would simplify code related to iterators, among other things.</p>
  
  <p>I know that for this to work, all <code>const</code> functions would have to be implicitly templated for <code>const</code>/non-<code>const</code> instantiations. So this is probably not possible.</p>
  
  <p><strong>EDIT:</strong>
  The original example falsely used <code>std::is_const_v&lt;decltype(*this)&gt;</code> and always returned <code>false</code>.</p>