12

I'm trying to use boost::adaptors::transformed by providing a c++0x lambda to the adaptor.

The following code does not compile. I'm using g++ 4.6.2 with 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;
}

Any ideas on what I'm doing wrong here?

4

3 回答 3

9

这是众所周知的问题。看这里

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 来启用自动结果类型推导,如下例所示。

于 2012-10-01T11:47:56.323 回答
4

好吧 lambdas 不能很好地发挥作用,因为它们不是默认可构造的,这对于迭代器来说是必需的。这是我用于 lambda 的包装器:

#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;
}
于 2012-10-01T15:22:26.767 回答
2

@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,这仍然是坏的。至少没有捕获的 lambda 应该满足的要求boost::transformed。)

如果 lambda 没有捕获(不是您的情况),则代码会更简单一些,或者您可以使用:

...
int(*my_ftor)(int) = [](int r)->int{return ...;}; // function pointer default constructible and callable
cout<<*br::max_element(a|badpt::transformed(my_ftor))<<endl;
...
于 2016-05-10T08:45:44.940 回答