gpt4 book ai didi

c++ - 访问带有额外参数的 boost 变体

转载 作者:太空狗 更新时间:2023-10-29 21:41:13 26 4
gpt4 key购买 nike

我正在尝试对 bool 公式实现 Tseitin 转换。这个想法是您使用转换规则转换 CNF 中的 bool 公式。例如,如果公式 f = l OR r,我们为每个公式分配一个新变量,比如说 v_f、v_l 和 v_r,然后我们将 f = l OR r 转换为 (! v_f 或 v_l 或 v_r)(v_f 或 !v_l)(v_f 或 !v_r)

我使用类似的东西(v_fv_lv_r 被替换为 pp1 p2m 是一个变量,它允许我们现在可以使用下一个变量):

int m = 0;
std::vector<std::vector<int> > formules;

struct op_or {};
struct op_and {};
struct op_not {};
struct op_impl {};

typedef int var;
template <typename tag> struct binop;
template <typename tag> struct unop;

typedef boost::variant<var,
boost::recursive_wrapper<unop <op_not> >,
boost::recursive_wrapper<binop<op_and> >,
boost::recursive_wrapper<binop<op_or> >,
boost::recursive_wrapper<binop<op_impl> >
> expr;

template <typename tag> struct binop
{
explicit binop(const expr& l, const expr& r) : oper1(l), oper2(r) { }
expr oper1, oper2;
};

template <typename tag> struct unop
{
explicit unop(const expr& o) : oper1(o) { }
expr oper1;
};

struct tseitin : boost::static_visitor<void>
{
tseitin() {}

void operator()(const var& v, int p = 0) {}
void operator()(const binop<op_and>& b, int p = m++) { proceed(0, b.oper1, b.oper2, p); }
void operator()(const binop<op_or>& b, int p = m++) { proceed(1, b.oper1, b.oper2, p);}
void operator()(const unop<op_not>& u, int p = m++) {}


void proceed(int nop, const expr& l, const expr& r, int p)
{
int p1 = m+1;
int p2 = m+2;
m += 2;
// Do the transformation
recurse(l, p1);
recurse(r, p2);
}

private:
template<typename T, typename U>
bool recurse(T const& v, U const& p)
{ return boost::apply_visitor(*this, v, p); }

};

主要是,我将此转换称为:boost::apply_visitor(tseitin(), result, 0);

但我遇到如下错误:error: no matching function for call to ‘apply_visitor(tseitin, expr&, int)’
boost::apply_visitor(tseitin(), 结果, 0);
并且:错误:请求'visitable'中的成员'apply_visitor',它是非类类型'const int'

我不太明白这个错误,你有什么想法吗?

您可能会注意到,我使用了 How to calculate boolean expression in Spirit并插入 spirit 教程实现运营商转型。

我需要完整的代码,现在让我来。

提前致谢!

最佳答案

I don't really understand the error, do you have any ideas ?

您正在调用 boost::variant::apply_visitor 的二进制版本。但是你给它传递了一个变体和一个整数。正如错误消息所表明的那样,一个 int 是不可访问的。

要完成这项工作,只需绑定(bind)参数:

boost::apply_visitor(boost::bind(tseitin(), _1, 0), result);

这意味着 _1 占位符将接收 variant 元素并传递第二个参数。

您还需要在 recurse 中执行此操作:

bool recurse(T const& v, U const& p) 
{ return boost::apply_visitor(boost::bind(*this, _1, p), v); }

更新

这是链接答案的演示,但使用额外的绑定(bind)参数而不是有状态的 printer 仿函数:

struct printer : boost::static_visitor<void>
{
//
void operator()(std::ostream& os, const var& v) const { os << v; }

void operator()(std::ostream& os, const binop<op_and>& b) const { print(os, " & ", b.oper1, b.oper2); }
void operator()(std::ostream& os, const binop<op_or >& b) const { print(os, " | ", b.oper1, b.oper2); }

void print(std::ostream& os, const std::string& op, const expr& l, const expr& r) const
{
os << "(";
boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), l);
os << op;
boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), r);
os << ")";
}

void operator()(std::ostream& os, const unop<op_not>& u) const
{
os << "(";
os << "!";
boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), u.oper1);
os << ")";
}
};

完整代码:

Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/variant/recursive_wrapper.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>

namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;

struct op_or {};
struct op_and {};
struct op_not {};

typedef std::string var;
template <typename tag> struct binop;
template <typename tag> struct unop;

typedef boost::variant<var,
boost::recursive_wrapper<unop <op_not> >,
boost::recursive_wrapper<binop<op_and> >,
boost::recursive_wrapper<binop<op_or> >
> expr;

template <typename tag> struct binop
{
explicit binop(const expr& l, const expr& r) : oper1(l), oper2(r) { }
expr oper1, oper2;
};

template <typename tag> struct unop
{
explicit unop(const expr& o) : oper1(o) { }
expr oper1;
};

struct eval : boost::static_visitor<bool>
{
eval() {}

//
bool operator()(const var& v) const
{
if (v=="T" || v=="t" || v=="true" || v=="True")
return true;
else if (v=="F" || v=="f" || v=="false" || v=="False")
return false;
return boost::lexical_cast<bool>(v);
}

bool operator()(const binop<op_and>& b) const
{
return recurse(b.oper1) && recurse(b.oper2);
}
bool operator()(const binop<op_or>& b) const
{
return recurse(b.oper1) || recurse(b.oper2);
}
bool operator()(const unop<op_not>& u) const
{
return !recurse(u.oper1);
}

private:
template<typename T>
bool recurse(T const& v) const
{ return boost::apply_visitor(*this, v); }
};

struct printer : boost::static_visitor<void>
{
//
void operator()(std::ostream& os, const var& v) const { os << v; }

void operator()(std::ostream& os, const binop<op_and>& b) const { print(os, " & ", b.oper1, b.oper2); }
void operator()(std::ostream& os, const binop<op_or >& b) const { print(os, " | ", b.oper1, b.oper2); }

void print(std::ostream& os, const std::string& op, const expr& l, const expr& r) const
{
os << "(";
boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), l);
os << op;
boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), r);
os << ")";
}

void operator()(std::ostream& os, const unop<op_not>& u) const
{
os << "(";
os << "!";
boost::apply_visitor(boost::bind(*this, boost::ref(os), _1), u.oper1);
os << ")";
}
};

bool evaluate(const expr& e)
{ return boost::apply_visitor(eval(), e); }

std::ostream& operator<<(std::ostream& os, const expr& e)
{ boost::apply_visitor(boost::bind(printer(), boost::ref(os), _1), e); return os; }

template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, expr(), Skipper>
{
parser() : parser::base_type(expr_)
{
using namespace qi;

expr_ = or_.alias();

or_ = (and_ >> '|' >> or_ ) [ _val = phx::construct<binop<op_or > >(qi::_1, qi::_2) ] | and_ [ _val = qi::_1 ];
and_ = (not_ >> '&' >> and_) [ _val = phx::construct<binop<op_and> >(qi::_1, qi::_2) ] | not_ [ _val = qi::_1 ];
not_ = ('!' > simple ) [ _val = phx::construct<unop <op_not> >(qi::_1) ] | simple [ _val = qi::_1 ];

simple = (('(' > expr_ > ')') | var_);
var_ = qi::lexeme[ +(alpha|digit) ];

BOOST_SPIRIT_DEBUG_NODE(expr_);
BOOST_SPIRIT_DEBUG_NODE(or_);
BOOST_SPIRIT_DEBUG_NODE(and_);
BOOST_SPIRIT_DEBUG_NODE(not_);
BOOST_SPIRIT_DEBUG_NODE(simple);
BOOST_SPIRIT_DEBUG_NODE(var_);
}

private:
qi::rule<It, var() , Skipper> var_;
qi::rule<It, expr(), Skipper> not_, and_, or_, simple, expr_;
};

int main()
{
const std::string inputs[] = {
std::string("true & false;"),
std::string("true & !false;"),
std::string("!true & false;"),
std::string("true | false;"),
std::string("true | !false;"),
std::string("!true | false;"),

std::string("T&F;"),
std::string("T&!F;"),
std::string("!T&F;"),
std::string("T|F;"),
std::string("T|!F;"),
std::string("!T|F;"),
std::string("") // marker
};

for (const std::string *i = inputs; !i->empty(); ++i)
{
typedef std::string::const_iterator It;
It f(i->begin()), l(i->end());
parser<It> p;

try
{
expr result;
bool ok = qi::phrase_parse(f,l,p > ';',qi::space,result);

if (!ok)
std::cerr << "invalid input\n";
else
{
std::cout << "result:\t" << result << "\n";
std::cout << "evaluated:\t" << evaluate(result) << "\n";
}

} catch (const qi::expectation_failure<It>& e)
{
std::cerr << "expectation_failure at '" << std::string(e.first, e.last) << "'\n";
}

if (f!=l) std::cerr << "unparsed: '" << std::string(f,l) << "'\n";
}

return 0;
}

关于c++ - 访问带有额外参数的 boost 变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29473978/

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