为什么 boost::spirit::qi::p arse() 没有设置这个 boost::variant 的值?
Why does boost::spirit::qi::parse() not set this boost::variant's value?
当尝试将文本解析为boost::变体时,变体的值不会被更改。解析器本身似乎工作得很好,所以我的假设是我在变体代码中做错了什么。
我使用boost 1.46.1和,下面的代码在 Visual Studio 2008中编译。
hkaiser注意到规则和语法模板参数不能是Variant
,而是Variant()
。
这有一点"进一步"。因为我现在有一个编译错误在boost_1_46_1boostvariantvariant.hpp(1304)
。注释说:
// NOTE TO USER :
// Compile error here indicates that the given type is not
// unambiguously convertible to one of the variant's types
// (or that no conversion exists).
显然,表达式(qi::double_ | +qi::char_)
的属性不是boost::variant<double, std::string>
。但那是什么呢?
2日更新
使用typedef boost::variant<double, std::vector<char>> Variant;
对解析器有效。但是,这并不像std::string…
#include <boost/spirit/include/qi.hpp>
#include <boost/variant.hpp>
int main()
{
namespace qi = boost::spirit::qi;
typedef std::string::const_iterator Iterator;
const std::string a("foo"), b("0.5");
// This works
{
std::string stringResult;
Iterator itA = a.begin();
const bool isStringParsed =
qi::parse(itA, a.end(), +qi::char_, stringResult);
double doubleResult = -1;
Iterator itB = b.begin();
const bool isDoubleParsed =
qi::parse(itB, b.end(), qi::double_, doubleResult);
std::cout
<< "A Parsed? " << isStringParsed <<
", Value? " << stringResult << "n"
<< "B Parsed? " << isDoubleParsed <<
", Value? " << doubleResult << std::endl;
// Output:
// A Parsed? 1, Value? foo
// B Parsed? 1, Value? 0.5
}
// This also works now
{
typedef boost::variant<double, std::vector<char>> Variant; // vector<char>, not string!
struct variant_grammar : qi::grammar<Iterator, Variant()> // "Variant()", not "Variant"!
{
qi::rule<Iterator, Variant()> m_rule; // "Variant()", not "Variant"!
variant_grammar() : variant_grammar::base_type(m_rule)
{
m_rule %= (qi::double_ | +qi::char_);
}
};
variant_grammar varGrammar;
Variant varA(-1), varB(-1);
Iterator itA = a.begin();
const bool isVarAParsed = qi::parse(itA, a.end(), varGrammar, varA);
Iterator itB = b.begin();
const bool isVarBParsed = qi::parse(itB, b.end(), varGrammar, varB);
// std::vector<char> cannot be put into std::cout but
// needs to be converted to a std::string (or char*) first.
// The conversion I came up with is very ugly but it's not the point
// of this question anyway, so I omitted it.
// You'll have to believe me here, when I'm saying it works..
// Output:
// A (variant): Parsed? 1, Value? foo, Remaining text = ''
// B (variant): Parsed? 1, Value? 0.5, Remaining text = ''
}
return 0;
}
规则的属性必须使用函数声明语法指定:
qi::rule<Iterator, Variant()> m_rule;
我没有尝试过,但我相信它会在这个改变后工作(同样是需要语法,顺便说一句)。
相关文章:
- 如何在boost beast http请求中设置http头
- 将更高的优先级设置为 boost::asio 线程处理进程
- 是否可以为boost::random::uniform_int_distribution<>设置确定性种子?
- boost::log 设置"Channel"通道记录器中的属性
- 浏览器将随机HTTP消息正文发送到我的boost.asio服务器.我可以更改此设置吗?
- 如何在 setter 方法中设置 boost::optional 成员变量的值?
- 如何使用Boost Graph库使用循环中的循环设置相同的边缘重量
- boost::p rogram_options:是否可以将一个选项设置为另一个选项的默认值
- Boost Python:指针设置为空
- 在Windows 8.1上安装Boost Python,正确设置工具链
- boost::iostreams::stream<boost::iostreams::array_source> 未设置 EOF
- BOOST LOCAL_DATE_TIME:如何设置特定时间信息(小时,最小值和SEC)
- 如何将serial_port flow_control设置为boost asio之外的其他方法
- 如何设置boost.log以限制日志文件的数量
- 在Boost :: Dynamic_bitset中提取设置的位索引
- Boost managed_mapped_file:设置允许的最大内存使用率
- C++ boost::shared_ptr 作为类的成员并重复设置其值
- 使用Boost Asio设置发布队列大小的限制
- 区域设置特定于boost区域设置的低成本问题
- c++, Mac OS X, Xcode 8: Compile Boost:设置部署目标为OS X 10.11