c++ - boost::lambda : _1 不是类或命名空间

标签 c++ boost stl lambda

我想从具有字符串作为属性(可用的公共(public) getter )的类列表中填充一组字符串。

我想使用 lambda 表达式和 std::for_each 来完成。

我在想类似的事情:

class Foo
{
    const std::string& getMe() const;
}

...
std::list<Foo> foos; // Let's image the list is not empty
std::set<std::string> strings; // The set to be filled

using namespace boost::lambda;
std::for_each(foos.begin(), foos.end(), bind(
    std::set<std::string>::insert, &strings, _1::getMe()));

但是,我在编译时遇到了这个错误:

_1 is not a class or namespace

谢谢。

最佳答案

正确的做法是:

class Foo
{
public:
    const void* getMe() const
    {
        return this;
    }
};

int main()
{
    std::list<Foo> foos(10);
    std::set<const void*> addresses; // The set to be filled

    using boost::bind;
    std::for_each(foos.begin(), foos.end(), bind(
        &std::set<const void*>::insert, &addresses, bind(&Foo::getMe, _1)));

    return 0;
}

关于c++ - boost::lambda : _1 不是类或命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14443793/

相关文章:

c++ - 没有 RTTI 的 std::any,它是如何工作的?

android/opencv - 从 NDK 中的应用程序文件夹加载级联文件

c++ - 静态链接的boost库的动态链接错误

c++ - 使用 boost::program_options 时,如何设置参数的名称?

c++ - 如何在 C++ 中更改 STL 容器的大小

c++ - 如何将 2 个 char* 数组直接映射到 std::map<std::string,std::string>

C++ 添加不工作

c++ - 这是数组初始化吗?

c++ - ostream_iterator 将数字数据写入文件的性能?

c++ - 递归函数中的boost regex smatch表现得像一个静态变量