Boost.Spirit与Boost.Variant和C ++ 11:期待零参数构造函数

时间:2012-04-13 08:13:16

标签: c++ boost c++11 boost-spirit boost-variant

我正在尝试使用Boost.Spirit编译一个简单的语法。我在Arch Linux x86_64上使用g ++ 4.7.0并提升1.49.0-1.1。

这里的最终目标是汇编程序。将有多个操作数,每个操作数一个类。所有操作数类型一起存储在boost::variant类型中。

我已成功将此样本编译为direct规则,当它也是语法的base_type时,但引入operand规则(并使其成为基本类型)会导致g ++ 4.7 .0抱怨:

example.cpp:61:7:   required from ‘Grammar<Iterator>::Grammar() [with Iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >]’
example.cpp:76:21:   required from here
/usr/include/boost/spirit/home/qi/detail/attributes.hpp:23:63: error: no matching function for call to ‘DirectOperand::DirectOperand()’
/usr/include/boost/spirit/home/qi/detail/attributes.hpp:23:63: note: candidates are:
example.cpp:20:12: note: DirectOperand::DirectOperand(const DirectValue&)
example.cpp:20:12: note:   candidate expects 1 argument, 0 provided
example.cpp:16:7: note: DirectOperand::DirectOperand(const DirectOperand&)
example.cpp:16:7: note:   candidate expects 1 argument, 0 provided

我不明白为什么它应该为DirectOperand寻找默认构造函数,因为语义操作应该用构造函数调用它。

我尝试了很多变化,包括

operand = directOp[_val = _1];

甚至编写一个辅助函数来“强制”该类型,如:

static Operand makeDirectOperand( const DirectOperand& op ) { return op; }

// ...

operand = directOp[&makeDirectOp];

但无论我做什么,都会抱怨缺少默认构造函数。

当我实际定义一个零参数构造函数时,我发现它已编译,但DirectOperand::value_从未改变我指定的默认值。

这是代码。它尽可能短。

#include <cstdint>
#include <iostream>
#include <string>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_uint.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/variant.hpp>

typedef std::uint16_t DataWord;
typedef boost::variant<std::string, DataWord> DirectValue;

class DirectOperand {
private:
  DirectValue value_;
public:
  explicit DirectOperand( const DirectValue& value ) :
  value_( value ) {}

  const DirectValue& value() const { return value_; }
};

// For example purposes, but there will be multiple operand types
// here.
typedef boost::variant<DirectOperand> Operand;

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

template <typename Iterator>
struct Grammar : qi::grammar<Iterator, Operand(), ascii::space_type> {
   Grammar() : Grammar::base_type( operand ) {
      using qi::lexeme;
      using ascii::char_;
      using qi::uint_parser;
      using namespace qi::labels;

      uint_parser<DataWord, 16, 1, 4> uhex_p;
      uint_parser<DataWord, 10, 1, 5> uint_p;

      word =
        char_( "a-zA-Z._" ) [_val += _1]
        >> *char_( "a-zA-Z0-9._" ) [_val += _1]
        ;

      number = (
        "0x" >> uhex_p
        | uint_p
        )
        [_val = _1]
        ;

      direct %= ( word | number );

      directOp %= direct;

      // This would be ( directOp | indirectOp | etc)
      operand %= directOp;
   }

  qi::rule<Iterator, DataWord(), ascii::space_type> number;
  qi::rule<Iterator, std::string()> word;
  qi::rule<Iterator, DirectValue(), ascii::space_type> direct;
  qi::rule<Iterator, DirectOperand(), ascii::space_type> directOp;
  qi::rule<Iterator, Operand(), ascii::space_type> operand;
};

int main() {
   std::string line;

   typedef std::string::iterator iterator_type;
   typedef Grammar<iterator_type> Grammar;
   Grammar grammar {};
}

1 个答案:

答案 0 :(得分:3)

我相信属性qi::rule(此处为directOp)的实例化需要默认构造函数。

如果您不想在DirectOperand中包含默认构造函数,可以尝试将其包装在boost::optional中,以便推迟初始化。