ブースト範囲アダプターの使用を理解しようとしてきましたが、私が見つけたすべての実用的な例は、プリミティブ型の 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_of
transformed
BOOST_RESULT_OF_USE_DECLTYPE
filtered
Thing