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

标签 c++ templates stl iterator

问题是:

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

template <typename IterType>
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) {
    // ...
}

有可能使 foo() 调用 my_really_large_implementation_specific_function() 而无需将 my_really_large_implementation_specific_function() 的代码包含在头文件中且无需制作其模板的多个实例?也许使用某种包装迭代器类,但我不确定如何使用。

最佳答案

在此处查看使用 Boost.Range 中包含的 any_iterator 的示例: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;

};

关于c++ - 如何在不将代码放入头文件的情况下处理任何迭代器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9744373/

相关文章:

c++ - 从 C++ 中的另一个源访问类,初始化构造函数时出现问题

c++ - 比较 std::function<>

c++ - Matlab编译C++文件报错: fatal error C1083: Cannot open include file: 'stdafx.h'

c++ - 代表C++类模板中的空类型

c++ - 模板类方法特化

c++ - 从 std::set C++ 中删除重复项

c++ - 删除指向 unordered_map 中对象的指针

c++ - 通过自由函数或成员函数进行扩展的机制

c++ - std::lower_bound 的比较函数

c++ STL map copy 将 bool 值设置为 true