title: Interfaces vs Templates for dependency injection in C++
body: <p>To be able to unit test my C++ code I usually pass the constructor of the class under test one or several objects that can be either "production code" or fake/mock objects (let's call these injection objects). I have done this either by</p>
  
  <ol>
  <li>Creating an interface that both the "production code" class and the fake/mock class inherits.</li>
  <li>Making the class under test a template class that takes the types of the injection objects as template parameters, and instances of the injection objects as parameters to the constructor.</li>
  </ol>
  
  <p>Some random thoughts:</p>
  
  <ul>
  <li>Until we have concepts (C++0x), only documentation and parameter naming will hint what to provide the class under test (when using templates).</li>
  <li>It is not always possible to create interfaces for legacy code</li>
  <li>The interface is basically only created to be able to do dependency injection</li>
  <li>In the same way: templating the class under test is done only to enable dependency injection</li>
  </ul>
  
  <p>What are your thoughts? Are there other solutions to this problem?</p>