When trying to parse text in boost :: variant, the value of the variant does not change. Parsers themselves work fine, so I guess I'm doing something wrong with the option code.
I am using boost 1.46.1 and the following code compiles in Visual Studio 2008.
First update
hkaiser noted that the arguments of the rule pattern and grammar should not be Variant, but Variant().
This has changed a bit since I now have a compilation error in boost_1_46_1\boost\variant\variant.hpp(1304). The comment says:
Thus, it is obvious that the attribute of the expression is (qi::double_ | +qi::char_)not boost::variant<double, std::string>. But then what?
Second update
typedef boost::variant<double, std::vector<char>> Variant; . std::string...
int main()
{
namespace qi = boost::spirit::qi;
typedef std::string::const_iterator Iterator;
const std::string a("foo"), b("0.5");
{
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;
}
{
typedef boost::variant<double, std::vector<char>> Variant;
struct variant_grammar : qi::grammar<Iterator, Variant()>
{
qi::rule<Iterator, Variant()> m_rule;
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);
}
return 0;
}