2012-03-16 9 views
0

は問題です:ヘッダファイルにコードを入れずにイテレータを処理する方法は?ここ

ヘッダファイル(ライブラリの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) { 
    // ... 
} 

です "my_really_large_implementation_specific_function()含めずfoo()コールmy_really_large_implementation_specific_function()を行うことができますヘッダーファイル内に、そのテンプレートの複数のインスタンスを作成することなく、たぶん、何らかのラッパー・イテレーター・クラスを使用していたかもしれませんが、どうしたらよいか分かりません。

+0

は、イテレータの値型は、共通の基本クラスを持っていますか? – ipc

+2

1つのインスタンスだけを必要とする場合はなぜテンプレートを使用しますか? –

+0

リンカーは、あなたの関数のインスタンスが1つだけになるほどスマートです。 –

答えて

1

関数を任意のイテレータ型で操作できるようにするには、本文をヘッダに表示する必要があります。

イテレータタイプをサポートする必要がある場合は、テンプレートである必要はなく、ソースファイルに表示することができます。

0

多分あなたは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); 
} 
+0

私の状況ではうまく動作しません。 – mtk358

2

あなたはboost::any_rangeを見ましたか? type erasureを使用して、仮想メソッド呼び出しのテンプレートタイプをトレードオフとして非表示にします。 Boost.Rangeに含まれているany_iteratorを使用してexempleはこちら

4

ルック: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; 

}; 
関連する問題