Two Questions About Lambda Use With boost::adaptors::filtered()

80 views Asked by At

Please consider this non-compiling code:

#include <boost/range/adaptors.hpp>

class Stuff {
 public:
  bool var;
};

class Manager {
 /// Get everything
 std::vector<Stuff*>
 get_all_stuff() const
 {
   return list_of_stuff;
 }

 /// Get a vector of only those that whose "var" matches the "tf" argument.
 std::vector<Stuff*>
 get_some_stuff(const bool tf) const
 {
   return (get_all_stuff() |
     boost::adaptors::filtered(
       [](Stuff const& s) { return (s.var == tf); }
     )
   );
 }

 private:
  std::vector<Stuff*> list_of_stuff;
};

Compilation dies with this error:

ex.cc: In lambda function:
ex.cc:21:46: error: ‘tf’ is not captured
        [](Stuff const& s) { return (s.var == tf); }
                                              ^

1.) How do I bring that function argument into my lambda?

2.) Is this a dangerous approach? Should I use std::remove_copy_if() instead?

  • I'm NOT worried about the lifetime of the vector returned by "get_all_stuff()".
  • I AM worried about the lifetime of the vector returned by "get_some_stuff()".
1

There are 1 answers

3
lakeweb On BEST ANSWER

To get an external value into a lambda, you have to capture it.

[&tf](Stuff const& s) { return (s.var == tf);

I used boost::adaptors::filter in the example. But ether one will return a range, not a vector object. If you want to return a vector that differs from list_of_stuff, you will have to build it. If you return it from your function the compiler will move it if possible. Here is a working example on coliru.

#include <iostream>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>

class Stuff {
public:
    bool var;
    int id;
};
std::ostream& operator << (std::ostream& os, const Stuff stuff) {
    return os << std::boolalpha << stuff.id << " " << stuff.var;
}
using vector_type = std::vector<Stuff>;

class Manager {
    /// Get everything
public:
    auto get_all_stuff() const
    {
        return list_of_stuff;
    }

    // Get a vector of only those that whose "var" matches the "tf" argument.
    vector_type get_some_stuff(const bool tf) const
    {
        vector_type temp;
        for (auto item : boost::adaptors::filter(list_of_stuff,
            [&tf](Stuff const& s) { return s.var == tf; }))
            temp.push_back(item);
        return temp;
    }

private:
    vector_type list_of_stuff = { {false,1},{true,2},{false,3},{true,4},{true,5} };
};
int main()
{
    Manager manage;
    for (const auto item : manage.get_all_stuff())
        std::cout << item << " ";
    std::cout << std::endl;
    for (const auto item : manage.get_some_stuff(true))
        std::cout << item << " ";
    std::cout << std::endl;
}