4

ブースト範囲アダプターの使用を理解しようとしてきましたが、私が見つけたすべての実用的な例は、プリミティブ型の STL コンテナーのみstd::list<int>を使用し、独自のクラスを使用しようとすると、すべてがバラバラになります。

#define BOOST_RESULT_OF_USE_DECLTYPE
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <boost/range/adaptors.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/algorithm.hpp>

struct Thing
{
  Thing() : _id(0), _name(""){}
  std::size_t _id;
  std::string _name;
};

int main()
{
  std::vector<Thing> input;
  std::vector<Thing> output;
  std::function<Thing (Thing&)> transform( [](Thing& t)->Thing{
    t._name = "changed";
    return t;});

  struct Filter
  {
    typedef bool result_type;
    typedef const Thing& argument_type;
    result_type operator()(const Thing& t)
    {
      return t._id > 1;
    }
  };
  Filter filter;

  boost::copy(input
      | boost::adaptors::filtered(filter)
      | boost::adaptors::transformed(transform)
      | boost::adaptors::reversed,
      output
      );
}

gcc 4.6/4.8 と boost 1.48/1.54/trunk を使用すると、次のコンパイル エラーが発生します。

/usr/include/c++/4.8/bits/stl_algobase.h:382:57: error: no type named ‘value_type’ in ‘struct std::iterator_traits<std::vector<Thing> >’
       typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
                                                         ^
/usr/include/c++/4.8/bits/stl_algobase.h:387:9: error: no type named ‘value_type’ in ‘struct std::iterator_traits<std::vector<Thing> >’
         && __are_same<_ValueTypeI, _ValueTypeO>::__value);

thisへの回答でアドバイスされているように、私の定義にもかかわらず、問題を引き起こす可能性がある問題を理解してdecltypeいます。ただし、ファンクター構造体を渡せない理由や、クラスに追加の要件があるかどうかはわかりません。result_oftransformedBOOST_RESULT_OF_USE_DECLTYPEfilteredThing

4

1 に答える 1

3

ドキュメントによると、最初の引数copyは範囲で、2 番目の引数はイテレータであるため、呼び出しを次のように変更します。

boost::copy(input
  | boost::adaptors::filtered(filter)
  | boost::adaptors::transformed(transform)
  | boost::adaptors::reversed,
  std::back_inserter(output)
  );

g++ 4.8.1 および boost 1.53.0 で問題なくコンパイルできます。

于 2013-07-07T14:21:18.193 に答える