请考虑这个非编译代码:
#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;
};
编译因此错误而死:
ex.cc: In lambda function:
ex.cc:21:46: error: ‘tf’ is not captured
[](Stuff const& s) { return (s.var == tf); }
^
1.) 如何将该函数参数带入我的 lambda?
2.) 这是一种危险的方法吗?我应该改用 std::remove_copy_if() 吗?
- 我不担心“get_all_stuff()”返回的向量的生命周期。
- 我担心“get_some_stuff()”返回的向量的生命周期。