gpt4 book ai didi

c++ - 切换到更高版本的 Boost 1.6.1 时出现编译错误

转载 作者:太空狗 更新时间:2023-10-29 21:12:09 25 4
gpt4 key购买 nike

我将我的 Boost 版本从 1.6.1 切换到 >=1.6.2 和我的 boost::spirit解析器代码无法编译。实际上,我认为这个问题与 Boost Variant 从 1.6.1 版到 1.6.2 版的错误修复有关。

版本 1.6.2 的发行说明说:

Variant constructors and assignment operators now do not participate in overload resolutions if variant can not hold the input type #5871, #11602

这是我的失败代码的精简版:

解析器.h

#pragma once
#include <string>
#include <boost/variant.hpp>

struct AccTag {};

template <typename tag> struct unop;

typedef unop<AccTag> Acc;

typedef boost::variant<
boost::recursive_wrapper<Acc>
> computationExpr;

typedef boost::variant<
boost::recursive_wrapper<computationExpr>,
int
> expr;

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

expr parse(const std::string& expression, bool& ok);

解析器.cpp

#include "Parser.h"

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

using namespace boost;

template <typename Iterator = std::string::iterator, typename Skipper = spirit::qi::space_type>
class ParserImpl : public spirit::qi::grammar<Iterator, expr(), Skipper>
{
public:

ParserImpl() : ParserImpl::base_type(expr_)
{
using namespace boost::spirit::qi;
using namespace boost::phoenix;

expr_ = props_.alias();

props_ = (
(lit("Acc") >> "(" >> int_ >> ")")[_val = construct<Acc>(_1) /* Most likely the source of the error */]
);

}

spirit::qi::rule<Iterator, expr(), Skipper> props_;
spirit::qi::rule<Iterator, expr(), Skipper> expr_;
};


expr parse(const std::string& expression, bool& ok)
{
expr result;
std::string formula = expression;
ParserImpl<> parser;
auto b = formula.begin();
auto e = formula.end();
ok = spirit::qi::phrase_parse(b, e, parser, spirit::qi::space, result);
if (b != e) {
ok = false;
}
return result;

}

代码在 1.6.1 版本中编译没有问题,但在 1.6.2 版本中失败并出现错误:

.../proto/transform/default.hpp(154): error C2679: Binary operator "=": ...

我猜在版本 1.6.1 中有一个从 computationExpr 的隐式转换至 expr ,这不再被允许。

如何修复此代码?我觉得 _val = construct<Acc>(_1) 里有东西必须改变,但我缺乏这样做的技能。

最佳答案

事实上,自 1.62 以来,recursive_wrapper 对隐式构造的选项进行了更多限制:

Wandbox on Boost 1.61

boost::variant<int, boost::recursive_wrapper<std::string> > x;
x = "worked before";
std::cout << boost::get<std::string>(x) << "\n";

Broken on Boost 1.62

boost::variant<int, boost::recursive_wrapper<std::string> > x;
x = "worked before";
std::cout << boost::get<std::string>(x) << "\n";

在这种情况下,很容易修复: Fixed on Boost 1.62

x = std::string("Hello world");

你的代码

在您的代码中,递归包装器的嵌套使用使事情变得复杂。好消息是,你不需要有两层。只需放下一个:

typedef boost::variant<
int,
computationExpr
> expr;

第二个递归包装器已经充分解耦了实例化。现在,一切都恢复正常了。

演示时间

注意一些样式修复/建议:

Also, I reordered the elements in the expr variant because they were triggering infinite recursion on default construction.

Live On Coliru

#pragma once
#include <string>
#include <boost/variant.hpp>

struct AccTag {};

template <typename> struct unop;
typedef unop<AccTag> Acc;

typedef boost::variant<
boost::recursive_wrapper<Acc>
> computationExpr;

typedef boost::variant<
int,
computationExpr
> expr;

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

expr parse(const std::string& expression, bool& ok);
#include "Parser.h"

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

namespace qi = boost::spirit::qi;

template <typename Iterator = std::string::const_iterator, typename Skipper = qi::space_type>
class ParserImpl : public qi::grammar<Iterator, expr(), Skipper>
{
public:

ParserImpl() : ParserImpl::base_type(expr_)
{
namespace phx = boost::phoenix;
using namespace qi;

expr_ = props_.alias();

props_ =
(lit("Acc") >> '(' >> int_ >> ')')[_val = phx::construct<Acc>(_1)]
;
}

private:
qi::rule<Iterator, expr(), Skipper> props_;
qi::rule<Iterator, expr(), Skipper> expr_;
};

expr parse(const std::string& formula, bool& ok)
{
expr result;
ParserImpl<> parser;
auto b = formula.begin();
auto e = formula.end();
ok = qi::phrase_parse(b, e, parser >> qi::eoi, qi::space, result);
return result;

}
static inline std::ostream& operator<<(std::ostream& os, Acc const& o) {
return os << "Acc(" << o.oper1 << ")";
}

int main() {
bool ok;
auto e = parse("Acc (3)", ok);

if (ok)
std::cout << "Parsed: " << e << "\n";
else
std::cout << "Parse failed\n";
}

打印

Parsed: Acc(3)

关于c++ - 切换到更高版本的 Boost 1.6.1 时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48097398/

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