gpt4 book ai didi

c++ - boost::phoenix try_catch_all 构造编译失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:52:18 26 4
gpt4 key购买 nike

我正在编写用于日期解析的 boost::spirit::qi 语法。

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

template < typename InputIterator >
struct date_rfc1123_grammar :
boost::spirit::qi::grammar< InputIterator, boost::gregorian::date()> {
typedef boost::gregorian::date value_type;
date_rfc1123_grammar() : date_rfc1123_grammar::base_type(date)
{
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
using qi::_pass;
using qi::_val;
using qi::_2;
using qi::_3;
using qi::_4;

_2digits = qi::uint_parser< std::uint32_t, 10, 2, 2 >();
_4digits = qi::uint_parser< std::uint32_t, 10, 4, 4 >();
date = (weekday >> ' ' >> _2digits >> ' ' >> month >> ' ' >> _4digits)
[
phx::try_[
_val = phx::construct< value_type >( _4, _3, _2 )
].catch_all[
_pass = false
]
];
}
boost::spirit::qi::rule< InputIterator, value_type()> date;
weekday_grammar weekday;
month_grammar month;
boost::spirit::qi::rule< InputIterator, std::int32_t() > _2digits;
boost::spirit::qi::rule< InputIterator, std::int32_t() > _4digits;
};

我依靠 boost::gregorian::date 构造函数进行参数检查,并希望解析器在出现异常时失败。但是 boost::phoenix::try_[ ].catch_all[ ] 构造无法编译,并显示以下消息:

/path_to_file/datetime_parse.hpp:102:8:   required from ‘tip::http::grammar::parse::date_rfc1123_grammar<InputIterator>::date_rfc1123_grammar() [with InputIterator = boost::spirit::multi_pass<std::istreambuf_iterator<char, std::char_traits<char> > >]’
/path_to_file/grammar_parse_test.hpp:17:7: required from here
/usr/local/include/boost/proto/traits.hpp:341:13: error: static assertion failed: 0 == Expr::proto_arity_c
BOOST_STATIC_ASSERT(0 == Expr::proto_arity_c);
^

没有 try_.catch_all 构造语法编译正常,但我希望解析器捕获异常并将 _pass 标志设置为 false 以使语法失败。

操作系统和编译器信息:

$ uname -a
Linux zmij 3.19.0-27-generic #29-Ubuntu SMP Fri Aug 14 21:43:37 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
$ g++ -v
Thread model: posix
gcc version 4.9.2 (Ubuntu 4.9.2-10ubuntu13)

提升版本 1.58

最佳答案

我以前见过这个,可能是某些增强/编译器版本的本地内容。

解决方法是包含一个空操作语句(例如 _pass=_pass)使其成为一个序列:

    date = (weekday >> ' ' >> _2digits >> ' ' >> month >> ' ' >> _4digits)
[
_pass = _pass,
phx::try_[
_val = phx::construct< value_type >( _4, _3, _2 )
].catch_all[
_pass = false
]
];

查看 Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/date_time/gregorian/greg_date.hpp>

template <typename InputIterator>
struct date_rfc1123_grammar : boost::spirit::qi::grammar< InputIterator, boost::gregorian::date()>
{
typedef boost::gregorian::date value_type;
date_rfc1123_grammar() : date_rfc1123_grammar::base_type(date)
{
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
using qi::_pass;
using qi::_val;
using qi::_2;
using qi::_3;
using qi::_4;

date = (weekday >> ' ' >> _2digits >> ' ' >> month >> ' ' >> _4digits)
[
_pass = _pass,
phx::try_[
_val = phx::construct< value_type >( _4, _3, _2 )
].catch_all[
_pass = false
]
];
}
boost::spirit::qi::rule< InputIterator, value_type()> date;
boost::spirit::qi::rule< InputIterator, uint()> weekday, month;
boost::spirit::qi::uint_parser< std::uint32_t, 10, 2, 2 > _2digits;
boost::spirit::qi::uint_parser< std::uint32_t, 10, 4, 4 > _4digits;
};

int main() {
using It = std::string::const_iterator;
std::string const input;

date_rfc1123_grammar<It> g;

It f = input.begin(), l = input.end();

boost::gregorian::date d;
bool ok = boost::spirit::qi::parse(f, l, g, d);

return ok?1:2;
}

关于c++ - boost::phoenix try_catch_all 构造编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32245224/

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