gpt4 book ai didi

c++ - 我的 Boost Phoenix lambda 有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-04 11:31:46 24 4
gpt4 key购买 nike

我认为 Phoenix lambda 函数在某种程度上是 C++11 lambda。所以我尝试以下操作:

http://coliru.stacked-crooked.com/a/38f1a2b655ea70fc

#include <boost/phoenix.hpp>
#include <iostream>
#include <ostream>

using namespace std;
using namespace boost;
using namespace phoenix;
using namespace arg_names;
using namespace local_names;


struct FakeOne{
int field;
};

int main()
{
auto k = FakeOne();

auto fn = (lambda(_a=k)[_a.field ]);
cout <<
fn()
<< endl;

}

抛出:

main.cpp:20:32: error: 'const _a_type' has no member named 'field'
auto fn = (lambda(_a=k)[_a.field ]);

最佳答案

您不能仅在占位符(如 _a)上调用成员,因为它们不声明成员(如 field)。相反,绑定(bind)它们:

auto fn = phx::bind(&FakeOne::field, k);

更新评论:

#include <boost/phoenix.hpp>

namespace phx = boost::phoenix;
using namespace phx::local_names;

struct FakeOne{
int field;
};

auto k = FakeOne { 3 };

int main()
{
auto fn = phx::bind(&FakeOne::field, k);

k.field = 99;
return fn();
}

编译为

main:                     ; test.cpp:13
movl k(%rip), %eax ; boost/boost/proto/expr.hpp:65
movl $99, k(%rip) ; test.cpp:16
ret

在 GCC -O3 上

关于c++ - 我的 Boost Phoenix lambda 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24478157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com