title: What is the difference between Inversion of Control and Dependency injection in C++?
body: <p>I've been reading recently about DI and IoC in C++. I am a little confused (even after reading related questions here on SO) and was hoping for some clarification.</p>
  
  <p>It seems to me that being familiar with the STL and Boost leads to use of dependency injection quite a bit. For example, let's say I made a function that found the mean of a range of numbers:</p>
  
  <pre><code>template &lt;typename Iter&gt;
  double mean(Iter first, Iter last)
  {
      double sum = 0;
      size_t number = 0;
      while (first != last)
      {
          sum += *(first++);
          ++number;
      }
      return sum/number;
  };
  </code></pre>
  
  <p>Is this (i.e., using iterators instead of accessing the collection itself) dependency injection? Inversion of control? Neither?</p>
  
  <p>Let's look at another example. We have a class:</p>
  
  <pre><code>class Dice
  {
  public:
      typedef boost::mt19937 Engine;
      Dice(int num_dice, Engine&amp; rng) : n_(num_dice), eng_(rng) {}
      int roll()
      {
          int sum = 0;
          for (int i = 0; i &lt; num_dice; ++i)
              sum += boost::uniform_int&lt;&gt;(1,6)(eng_);
          return sum;
      }
  private:
      Engine&amp; eng_;
      int n_;
  };
  </code></pre>
  
  <p>This seems like dependency injection. But is it inversion of control?</p>
  
  <p>Also, if I'm missing something, can someone help me out? This seems to be the natural way to do things, so if that's all there is to Dependency Injection, why do people have a hard time using it?</p>