2012-05-04 76 views
1

我不是很確定爲什麼下面的代碼在GCC 4.6.3中給我下面的錯誤:如何在boost :: spirit語義動作中將函數對象的結果賦值給本地

' operator ='in'boost :: spirit :: _ a = boost :: phoenix :: function :: operator()(const A0 &)const [with A0 = boost :: phoenix :: actor>,F = make_line_impl,typename boost :: phoenix :: as_composite,F,A0> :: type = boost :: phoenix :: composite,boost :: fusion :: vector,boost :: spirit :: argument < 0>,boost :: fusion :: void_, boost :: fusion :: void_,boost :: fusion :: void_,boost :: fusion :: void_,boost :: fusion :: void_,boost :: fusion :: void_,boost :: fusion :: void_,boost: :fusion :: void_ >>>((* & boost :: spirit :: _ 1))'

它甚至可以將一個惰性函數對象的結果賦給一個qi佔位符?

#include <string> 
#include <boost/spirit/include/phoenix_function.hpp> 
#include <boost/spirit/include/qi.hpp> 

using std::string; 

using boost::spirit::qi::grammar; 
using boost::spirit::qi::rule; 
using boost::spirit::qi::space_type; 
using boost::spirit::qi::skip_flag; 
using boost::spirit::unused_type; 

namespace qi = boost::spirit::qi; 
namespace phx = boost::phoenix; 

struct make_line_impl 
{ 
    int* _context; 
    make_line_impl(int* context) 
    { 
    _context = context; 
    } 

    template <typename Sig> 
    struct result; 

    template <typename This, typename Arg> 
    struct result<This(Arg const &)> 
    { 
    typedef int* type; 
    }; 
    template <typename Arg> 
    int* operator()(Arg const & content) 
    { 
    return new int(5); 
    } 
}; 


template<typename Iterator> 
struct MyGrammar : grammar<Iterator, unused_type, space_type> 
{ 
    rule<Iterator, unused_type, space_type> start; 
    rule<Iterator, int*(), space_type> label; 
    rule<Iterator, string*(), qi::locals<int*>, space_type> line; 

    MyGrammar() : MyGrammar::base_type(start) 
    { 
    make_line_impl mlei(new int(5)); 
    phx::function<make_line_impl> make_line(mlei); 

    start = *(line); 

    line = label[qi::_a = make_line(qi::_1)]; 
    } 
}; 


int main(int argc, char **argv) { 

    string contents; 

    qi::phrase_parse(contents.begin(), contents.end(), MyGrammar<string::iterator>(), space_type(), skip_flag::postskip); 
    return 0; 
} 

回答

2

我有固定在代碼中的一些點,使其編譯:

  • 我根據BOOST_RESULT_OF文檔改寫了嵌套::result<>::type邏輯。

    注意如果編譯在C + + 11模式下,你可能會更好過定義

    #define BOOST_RESULT_OF_USE_DECLTYPE 
    

    在這種情況下,你不必與嵌套的結果類型模板打擾。

  • 爲const

生成的代碼所需要的operator(...)方法:

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

using std::string; 

using boost::spirit::qi::grammar; 
using boost::spirit::qi::rule; 
using boost::spirit::qi::space_type; 
using boost::spirit::qi::skip_flag; 
using boost::spirit::unused_type; 

namespace qi = boost::spirit::qi; 
namespace phx = boost::phoenix; 

struct make_line_impl 
{ 
    int* _context; 
    make_line_impl(int* context) 
    { 
    _context = context; 
    } 

    template <typename Arg> struct result { typedef int* type; }; 

    template <typename Arg> 
    int* operator()(Arg const & content) const 
    { 
    return new int(5); 
    } 
}; 


template<typename Iterator> 
struct MyGrammar : grammar<Iterator, unused_type, space_type> 
{ 
    rule<Iterator, unused_type, space_type> start; 
    rule<Iterator, int*(), space_type> label; 
    rule<Iterator, string*(), qi::locals<int*>, space_type> line; 

    MyGrammar() : MyGrammar::base_type(start) 
    { 
    make_line_impl mlei(new int(5)); 
    phx::function<make_line_impl> make_line(mlei); 

    start = *(line); 

    line = label[qi::_a = make_line(qi::_1)]; 
    } 
}; 


int main(int argc, char **argv) { 

    string contents; 

    qi::phrase_parse(contents.begin(), contents.end(), MyGrammar<string::iterator>(), space_type(), skip_flag::postskip); 
    return 0; 
} 
相關問題