質問

テンプレートクラスを使用して変換することができない理由を見つけるのに苦労しています。

テンプレートクラスの簡易バージョンは次のとおりです。

template<typename T>
class base
{
public :
  base() : all_() {}
  ~base() {}
public:
  bool add(T t)
  {
    typename vector<T>::iterator itr 
      = lower_bound(all_.begin(), all_.end(), t);
    if ( itr == all_.end() || *itr != t )
      {
        all_.push_back(t);
        cout << "ok" << endl;
        return true;
      }
    cout << "failed" << endl;
    return false;
  }
  static bool addTo(base<T> *c, T t)
  {
    return c->add(t);
  }
private :
  vector<T> all_;
};

そして、ここでトランスフォームを使用して、メンバー追加関数からすべてのブール出力をキャプチャしようとしています:

main()
{
  base<int> test;
  vector<bool> results;
  vector<int> toAdd;
  toAdd.push_back(10);
  toAdd.push_back(11);
  toAdd.push_back(10);
  transform( toAdd.begin(), toAdd.end(),
             back_inserter(results),
             bind1st( (bool(*)(base<int>*,int))base<int>::addTo, &test ) );
}

目的は、base :: addまたはbase :: addToのいずれかを使用してtoAddコンテナーの各メンバーを挿入し、boolの結果をベクターの結果にキャプチャすることです

役に立ちましたか?

解決

試してください:

  transform( toAdd.begin(), toAdd.end(),
         back_inserter(results),
         bind1st( mem_fun(&base<int>::add), &test ) );

問題はテンプレートではなく、bind1stが動作するための特別なサポートに依存していることです( http://www.sgi.com/tech/stl/AdaptableBinaryFunction.html )。私の知る限り、古い関数ポインタを操作することはできません。

boost :: bind は、必要に応じてさらに多くの機能を実行できます。この状況では、必要ありませんが、 mem_fun は、 -静的メンバー関数から適応可能なバイナリ関数へ。したがって、 addTo も必要ありませんが、同様の状況で静的メンバー関数を使用する必要がある場合は、 ptr_fun があります。

他のヒント

次を基本クラスに追加します。

typedef base<T>* first_argument_type;
typedef T second_argument_type;
typedef bool result_type;

bool operator () (base<T> *c, T t) const {
    return c->add(t);
}

そして変換を次のように変更します:

transform(toAdd.begin(), toAdd.end(),
          back_inserter(results), bind1st(base<int>(), &test ));
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top