title: how to check if a class has an operator []?
body: <p>I'm trying to check if <code>operator []</code> overloaded in some class. There is my code:</p>
  
  <pre><code>template &lt;class T, class = void&gt;
  struct has_operator : std::false_type {};
  
  template &lt;class T&gt;
  struct has_operator &lt; T,
              std::void_t&lt;decltype(std::declval&lt;T&gt;[](int i))&gt;&gt; :
              std::true_type {};
  
  </code></pre>
  
  <p>But it doesn't work actualy.</p>
  
  <p>I'm trying to write like code witch cheking if class has <code>operator ()</code></p>
  
  <p>here it's :</p>
  
  <pre><code>template &lt;typename T,class=void&gt;
  struct callable_without_args: std::false_type{};
  
  template &lt;typename T&gt;
  struct callable_without_args&lt;T
            ,std::void_t&lt;decltype(std::declval&lt;T&gt;()())&gt;&gt;
    :std::true_type{};
  </code></pre>
  
  <hr>
  
  <p>Ok, This code compiles, but how to do the verification exactly in main?:</p>
  
  <pre><code>
  template &lt;class T, class = void&gt;
  struct has_operator : std::false_type {};
  
  template &lt;class T&gt;
  struct has_operator &lt; T,
              std::void_t&lt;decltype(std::declval&lt;T&gt;()[0])&gt;&gt; :
              std::true_type {};
  </code></pre>
  
  <hr>
  
  <p>I did it!</p>
  
  <pre><code>#include &lt;type_traits&gt;
  #include &lt;tuple&gt;
  #include &lt;vector&gt;
  
  
  template &lt;class T, class = void&gt;
  struct has_operator : std::false_type {};
  
  template &lt;class T&gt;
  struct has_operator &lt; T,
              std::void_t&lt;decltype(std::declval&lt;T&gt;()[0])&gt;&gt; :
              std::true_type {};
  
  
  struct T {
      int* arr;
      T(int a) {
          arr = new int[a];
      }
      const int&amp; operator [] (int i) {
          return arr[i];
      }
  };
  
  int main() {
      bool test = has_operator&lt;T&gt;::value;
  
      test == true ? std::cout &lt;&lt; "yes" : std::cout &lt;&lt; "no";
  }
  
  </code></pre>