title: Detect if variable provides a function in template
body: <p>I have a function template which is called with different values, i.e.</p>
  
  <pre><code>template &lt;typename T&gt;
  T do_calculations(const T &amp;var)
  {
       T return_variable;
       //Do calculations
       return return_variable;
  }
  </code></pre>
  
  <p>Now the calculations depend on the type of the variable <code>var</code>. <code>T</code> can either be a scalar (such as <code>double</code> or <code>int</code>), a vector (such as <code>std::vector&lt;&gt;</code> or other types) or a matrix. I know that I can differentiate between for example a <code>std::vector</code> and a <code>double</code> by knowing that <code>double</code> does not have a member function called <code>size()</code>. How can I implement that in my code above? My aim is to have similar code to this:</p>
  
  <pre><code>template &lt;typename T&gt;
  T do_calculations(const T &amp;var)
  {
       T return_variable;
       if(T.has_function_size())
           //Do calculations on vector
       else
           //Do calculations on scalar
       return return_variable;
  }
  </code></pre>
  
  <p>I can not do a simple check if <code>T</code> is a known type due to having a lot of different input types, but I know that all possible types having a function <code>size()</code> behave similar enough for me.</p>
  
  <p>Now my problem is:<br>
  My code for the vector calculation loops over all elements, but the code for scalar calculation only over the single element:</p>
  
  <pre><code>template &lt;typename T&gt;
  T do_calculations(const T &amp;var)
  {
       T return_variable;
       if(T.has_function_size())
           for(size_t i = 0; i &lt; T.size(); ++i)
               return_variable[i] = func(var[i]);
       else
           return_variable = func(var);
       return return_variable;
  }
  </code></pre>
  
  <p>with <code>func()</code> a function taking a scalar.<br>
  Compilation fails with the error that I can not apply <code>func()</code> to a vector (or my own type). Do I have to create separate functions for that, or can I tell the compiler that I know what I do?</p>