C++ 如何在不将代码放入头文件的情况下处理任何迭代器?

C++ 如何在不将代码放入头文件的情况下处理任何迭代器?,c++,templates,stl,iterator,C++,Templates,Stl,Iterator,问题是: 头文件(库API的一部分): 模板 void foo(const IterType&begin、const IterType&end); CPP文件: template <typename IterType> void my_really_large_implementation_specific_function(const IterType &begin, const IterType &end) { // ... } 模板 void my_

问题是:

头文件(库API的一部分):

模板
void foo(const IterType&begin、const IterType&end);
CPP文件:

template <typename IterType>
void my_really_large_implementation_specific_function(const IterType &begin, const IterType &end) {
    // ...
}
模板
void my_really_大型_实现_特定的_函数(const IterType&begin、const IterType&end){
// ...
}

是否可以在头文件中不包含
myreally\u large\u implementation\u specific\u函数()
而调用
myreally\u large\u implementation\u specific\u函数()
的代码,并且不生成其模板的多个实例?可能使用某种包装器迭代器类,但我不确定如何使用。

如果希望函数能够对任意迭代器类型进行操作,则正文需要显示在标题中


如果您只需要支持一种迭代器类型,那么它不需要是模板,并且可以显示在源文件中。

也许您只需要为每个迭代器类型执行一次操作

// 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);
}
//在标题中:
void my_really_large_实现_特定的_函数(const common_base_class&c);
样板
void foo(常量IterType&begin、常量IterType&end)
{
std::for_each(开始、结束、我的_真的_大的_实现_特定的_函数);
}

你看了吗?它使用虚拟方法调用来隐藏模板类型,作为一种折衷办法。

在这里查找使用Boost中包含的任何迭代器的示例。范围:

#包括
//请注意,这里没有包含多个索引!
#包括
#包括
#包括
类隐藏容器//向前声明隐藏的容器
类示例{
公众:
boost::scoped_ptr impl;//指针后面隐藏的容器
//声明一个类型擦除迭代器,该迭代器可以迭代std::string的任何容器
//这可以是std::vector::const_迭代器或std::list::const_迭代器
typedef boost::range\u detail::any\u迭代器<
常量std::字符串,
boost::前向遍历标记,
常量std::字符串&,
标准:ptrdiff\t
>常量迭代器;
//执行器
例();
//抽象迭代器
常量迭代器begin()常量;
常量迭代器end()常量;
};

迭代器的值类型是否有一个公共基类?如果只需要一个实例,为什么要使用模板?链接器非常聪明,最终只会得到一个函数实例。如果您知道将与它一起使用的所有类型,您可以在cpp文件中定义模板化函数,并为所有类型实例化它。这将确保目标代码已创建并且在链接时可用。@ipc:如果“值类型”指的是迭代的类型,则为“是”。
// 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);
}
#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;

};