Q: How to handle any iterator without putting the code in the header file? Here is the problem:
Header file (part of a library's API):
template <typename IterType>
void foo(const IterType &begin, const IterType &end);

CPP file:
template <typename IterType>
void my_really_large_implementation_specific_function(const IterType &begin, const IterType &end) {
    // ...
}

Is is possible to make foo() call my_really_large_implementation_specific_function() without including my_really_large_implementation_specific_function()'s code in a header file and without making more than one instance of its template? Maybe using some kind of wrapper iterator class, but I'm not sure how.

A: Look here for an exemple using any_iterator that is included in Boost.Range : http://geek-cpp.blogspot.fr/2012/11/using-boost-anyiterator-to-hide.html
#include <string>
// note that there is no include to multi_index here! 
#include <boost/scoped_ptr.hpp>
#include <boost/range/concepts.hpp>
#include <boost/range/detail/any_iterator.hpp>

class hidden_container; //forward declare the hidden container

class exemple {
public:
  boost::scoped_ptr<hidden_container> impl; //hidden container behind pointer

  // declare a type erased iterator that can iterate over any container of std::string
  // this could be a std::vector<std::string>::const_iterator or a     std::list<std::string>::const_iterator
  typedef boost::range_detail::any_iterator<
         const std::string,
         boost::forward_traversal_tag,
         const std::string&,
         std::ptrdiff_t
         > const_iterator;

  //ctor
  exemple();
  // abstracted iterators
  const_iterator begin() const;
  const_iterator end() const;

};


A: Have you had a look at boost::any_range? It uses type erasure to hide template types with virtual method calls as a trade-off.

A: If you want your function to be able to operate on arbitrary iterator types then the body needs to appear in the header.
If you need to support only one iterator type then it doesn't need to be a template and can appear in the source file.

A: Maybe you simply want to do a for_each?
// In the header:
void my_really_large_implementation_specific_function(const common_base_class& c);

template <typename IterType>
void foo(const IterType &begin, const IterType &end)
{
   std::for_each(begin, end, my_really_large_implementation_specific_function);
}

