title: When to prefer explicit capture in lambda over implicit captures?
body: <p>There are sometimes cases where I do a <code>std::find_if</code> (for example) in a local function that has 5 local variables, including parameters. However, the lambda I pass into the STL algorithm only needs access to 1 of those. I could capture this in one of two ways:</p>
  
  <pre><code>void foo(int one, int two, int three)
  {
      std::vector&lt;int&gt; m_numbers;
      int four, five;
  
      std::find_if(m_numbers.begin(), m_numbers.end(), [=](int number) {
          return number == four;
      });
  }
  </code></pre>
  
  <p>Or I can do:</p>
  
  <pre><code>void foo(int one, int two, int three)
  {
      std::vector&lt;int&gt; m_numbers;
      int four, five;
  
      std::find_if(m_numbers.begin(), m_numbers.end(), [four](int number) {
          return number == four;
      });
  }
  </code></pre>
  
  <p>(Note I didn't compile this code, apologies for any syntax errors or other mistakes)</p>
  
  <p>I know that implicit captures are based on <em>odr-used</em> rules, so functionally and implementation-wise, I think both are identical. When would you use explicit captures over implicit ones? My only thought is somewhat related to principles of encapsulation: Having access to only the stuff you need allows the compiler to help you determine when you access a variable you shouldn't. It also keeps the local state of the method (it's invariants, for the lifetime of the function during its execution) safer. But are these really practical concerns?</p>
  
  <p>Are there functional reasons to use explicit captures over implicit ones? What is a good rule of thumb or best practice to follow?</p>