Итератор повышения и С++ 11 лямбда
Я пытаюсь использовать boost:: adapters:: transform, предоставляя адаптер x ++ 0x для адаптера.
Следующий код не компилируется. Я использую g++ 4.6.2 с boost 1.48.
#include <iostream>
#include <vector>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
using namespace std;
namespace br = boost::range;
namespace badpt = boost::adaptors;
int main()
{
vector<int> a = {0,3,1,};
vector<int> b = {100,200,300,400};
auto my_ftor = [&b](int r)->int{return b[r];};
cout<<*br::max_element(a|badpt::transformed(my_ftor))<<endl;
}
Любые идеи о том, что я здесь делаю неправильно?
Ответы
Ответ 1
Ну, lambdas не играют хорошо, поскольку они не являются конструктивными по умолчанию, что необходимо для итераторов. Вот обертка, которую я использую для лямбда:
#define RETURNS(...) -> decltype(__VA_ARGS__) { return (__VA_ARGS__); }
template<class Fun>
struct function_object
{
boost::optional<Fun> f;
function_object()
{}
function_object(Fun f): f(f)
{}
function_object(const function_object & rhs) : f(rhs.f)
{}
// Assignment operator is just a copy construction, which does not provide
// the strong exception guarantee.
function_object& operator=(const function_object& rhs)
{
if (this != &rhs)
{
this->~function_object();
new (this) function_object(rhs);
}
return *this;
}
template<class F>
struct result
{};
template<class F, class T>
struct result<F(T)>
{
typedef decltype(std::declval<Fun>()(std::declval<T>())) type;
};
template<class T>
auto operator()(T && x) const RETURNS((*f)(std::forward<T>(x)))
template<class T>
auto operator()(T && x) RETURNS((*f)(std::forward<T>(x)))
};
template<class F>
function_object<F> make_function_object(F f)
{
return function_object<F>(f);
}
Тогда вы можете просто сделать это:
int main()
{
vector<int> a = {0,3,1,};
vector<int> b = {100,200,300,400};
cout<<*br::max_element(a|badpt::transformed(make_function_object([&b](int r)->int{return b[r];};)))<<endl;
}
Ответ 2
Это хорошо известная проблема. Посмотрите здесь
http://boost.2283326.n4.nabble.com/range-cannot-use-lambda-predicate-in-adaptor-with-certain-algorithms-td3560157.html
Вскоре вы должны использовать этот макрос
#define BOOST_RESULT_OF_USE_DECLTYPE
для использования decltype
вместо boost::result_of
.
Цитата из здесь
Если ваш компилятор поддерживает decltype, вы можете включить автоматическое вычет типа результата путем определения макроса BOOST_RESULT_OF_USE_DECLTYPE, как в следующем примере.
Ответ 3
Ответ @ForEver (#define BOOST_RESULT_OF_USE_DECLTYPE
) не работал у меня.
И @Paul ответ слишком длинный (и слишком общий).
Более конкретным решением может быть следующее:
#include <iostream>
#include <vector>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
using namespace std;
namespace br = boost::range;
namespace badpt = boost::adaptors;
int main()
{
vector<int> a = {0,3,1,};
vector<int> b = {100,200,300,400};
struct{
vector<int>* bP; //pointer, just to imitate lambda syntax...
int operator()(int r) const{return (*bP)[r];} //was my_ftor = [&b](int r)->int{return b[r];};
} my_ftor{&b}; //...here
cout<<*br::max_element(a|badpt::transformed(my_ftor))<<endl;
}
(Это 2016, Boost 1.58, и это все еще сломано. По крайней мере, лямбда без захвата должна соответствовать требованиям boost::transformed
.)
Если у лямбда не было захвата (не ваше дело), код был бы немного проще или вы могли бы использовать:
...
int(*my_ftor)(int) = [](int r)->int{return ...;}; // function pointer default constructible and callable
cout<<*br::max_element(a|badpt::transformed(my_ftor))<<endl;
...