使用本地人

时间:2017-08-30 17:35:29

标签: boost boost-spirit locals qi

当my_rule有一些自定义类型的局部变量时,我无法在调试模式下编译代码(带有BOOST_SPIRIT_DEBUG_NODE(my_rule)的代码)。

  • 规则qi::locals<std::string>的第一个版本正常
  • 规则qi::locals<std::string,int>的第二个版本仍然正常
  • 规则qi::locals<std::string,std::vector<int> >的当前版本无法编译。

错误:operator<<不匹配(操作数类型为std::basic_ostream<char>const std::vector<int>

我声明了流媒体operator<<

std::ostream& >    operator<< (std::ostream& os, std::vector<int> const& art)

但它仍然无法编译。

我使用boost 1_64_0。这是最小的完整代码:

#define BOOST_SPIRIT_DEBUG
#if !defined(BOOST_SPIRIT_DEBUG_OUT)
#define BOOST_SPIRIT_DEBUG_OUT std::cerr
#endif

#include <tuple>

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.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/spirit/include/phoenix_object.hpp>

#include <iostream>
#include <string>
#include <vector>
// To solve the pb of declaration of grammar with locals
#include <typeinfo>

std::ostream& 
   operator<< (std::ostream& os, std::vector<int> const& art)
   {
       os << "[";
       for( auto it = art.begin(); it != art.end() ; it++ ) {
         os << *it << ",";
       }
       os << "]";
       return os;
   }

namespace client
{
   namespace phoenix = boost::phoenix;
   namespace qi = boost::spirit::qi;
   namespace ascii = boost::spirit::ascii;
   using phoenix::val;
   using namespace qi::labels;
   using qi::_val;
   using qi::_1;

   //  Our number list parser
   template <typename Iterator>
   struct mini_wkart_grammar
     // first version: : qi::grammar<Iterator, int(), qi::locals<std::string>, ascii::space_type>
      // second version: : qi::grammar<Iterator, int(), qi::locals<std::string,int>, ascii::space_type>
       : qi::grammar<Iterator, std::vector<int>(), qi::locals<std::string,std::vector<int> >, ascii::space_type>
   {
        mini_wkart_grammar() : mini_wkart_grammar::base_type(start,"numbers")
        {
           using phoenix::push_back;
          // first version: start= (qi::int_ >> qi::char_(',') >> qi::int_)[_val=_1+_3];
          // second version: start= (qi::int_[_b=_1] >> qi::char_(',') >> qi::int_[_b+=_1])[_val=_b];
          start= (qi::int_[push_back(_b,_1)] >> qi::char_(',') >> qi::int_[push_back(_b,_1)])[_val=_b];
          BOOST_SPIRIT_DEBUG_NODE(start);
       }
       // first version OK: qi::rule<Iterator, int(), qi::locals<std::string>, ascii::space_type> start;
       // second version OK: qi::rule<Iterator, int(), qi::locals<std::string,int>, ascii::space_type> start;
       qi::rule<Iterator, std::vector<int>(), qi::locals<std::string,std::vector<int> >, ascii::space_type> start;

    };
}

////////////////////////////////////////////////////////////////////////////
//  Main program
////////////////////////////////////////////////////////////////////////////
int
main()
{
    std::cout << "/////////////////////////////////////////////////////////\n\n";
    std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
    std::cout << "/////////////////////////////////////////////////////////\n\n";

    std::cout << "Give me a comma separated list of numbers.\n";
    std::cout << "Type [q or Q] to quit\n\n";

    // std::string result;
    // first ans second version: int result;
    std::vector<int> result;
    std::string str;
    using boost::spirit::ascii::space;

    client::mini_wkart_grammar<std::string::const_iterator> wkart_grammar;

   while (getline(std::cin, str))
   {
       if (str.empty() || str[0] == 'q' || str[0] == 'Q')
           break;
       std::string::const_iterator iter = str.begin();
       std::string::const_iterator end = str.end();

       // if (client::parse_numbers(str.begin(), str.end()))
       if (boost::spirit::qi::phrase_parse(iter, end, wkart_grammar, space, result))
       {
           std::cout << "-------------------------\n";
           std::cout << "Parsing succeeded\n";
           std::cout << result << " Parses OK: " << std::endl;
       }
       else
{
           std::cout << "-------------------------\n";
           std::cout << "Parsing failed\n";
          std::cout << "-------------------------\n";
      }
   }

   std::cout << "Bye... :-) \n\n";
   return 0;
}

我想在操作员声明中遗漏了什么?

感谢您的帮助..

1 个答案:

答案 0 :(得分:0)

First Off...

What are you trying to achieve anyways? That whole grammar could be $username = 'test@gmail.com'; $json = json_encode( array( "username" => $username, "send_welcome" => true, "welcome_message" => "Test", "account_attributes" => array( "email_address" => "testuser5@so.com", "first_name" => "Snow", "last_name" => "White", "middle_initial" => "A", "phone_number" => "2055551234", "address" => "1 Main St", "city" => "Detroit", "state" => "MI", "zip" => "48220", "country" => "US" ), "Gender" => "Male", "portals" => [], "groups" => [], "identification_card_numbers" => [] ) ); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); and still have the exact same effect. See Boost Spirit: “Semantic actions are evil”?

Your Question:

Sadly you need to make that import {functionName, functionName1, functionName2} from '../folder/filename.jsx' ADL-enabled. (http://en.cppreference.com/w/cpp/language/adl)

Since the element type is primitive, there is no associated namespace. So the only namespace that will be tried is var number = functionName(); which declared start = qi::int_ % ',';.

operator<<

That might have undesired side effects, you you may want to force the issue with a hack:

namespace ::std

See it Live On Wandbox