问题是:
头文件(库 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/