是的,phx :: bind和phx :: function <>默認情況下會按值打包它們的包裝器。然而。
比方說[1],你有一個錯誤處理這樣的...簡約例如:
template <typename=void> struct my_error_handler {
my_error_handler() = default;
my_error_handler(my_error_handler const&) = delete;
template<typename...> struct result { typedef void type; };
template<typename... T> void operator()(T&&...) const {
std::cerr << "my_error_handler invoked " << proof++ << "\n";
}
mutable int proof = 0;
};
(正如你可以看到我做了明確地不可複製的,以確保編譯器。不會產生默默地在我背後的代碼)
現在,我不知道這是否是你不小心錯過了一個組合,但是:
on_error<fail>(function, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
on_error<fail>(start, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
on_error<fail>(gr_instruction, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
工作得很好,一樣
auto ll = phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3);
on_error<fail>(function, ll);
on_error<fail>(start, ll);
on_error<fail>(gr_instruction, ll);
注意,因爲bind
需要一個參考,我們需要確保的err_handler
匹配壽命(或超過)解析器的,所以我做了err_handler
解析器類的成員。
當我將它傳遞輸入失敗,我的程序就能夠打印my_error_handler
的調用的proof
:
std::cout << "The 'proof' in the err_handler instance is: " << p.err_handler.proof << "\n";
像往常一樣,一個完全集成的例子程序:
#define BOOST_SPIRIT_USE_PHOENIX_V3
//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
namespace asmast
{
typedef std::string label;
}
template <typename=void> struct my_error_handler {
my_error_handler() = default;
my_error_handler(my_error_handler const&) = delete;
template<typename...> struct result { typedef void type; };
template<typename... T> void operator()(T&&...) const {
std::cerr << "my_error_handler invoked " << proof++ << "\n";
}
mutable int proof = 0;
};
template <typename It, typename Skipper = qi::blank_type>
struct parser : qi::grammar<It, Skipper>
{
parser() :
parser::base_type(start)
{
using namespace qi;
start = lexeme["Func" >> !(alnum | '_')] > function;
function = gr_identifier
>> "{"
>> -(
gr_instruction
| gr_label
//| gr_vardecl
//| gr_paramdecl
) % eol
> "}";
gr_instruction_names.add("Mov", unused);
gr_instruction_names.add("Push", unused);
gr_instruction_names.add("Exit", unused);
gr_instruction = lexeme [ gr_instruction_names >> !(alnum|"_") ] > gr_operands;
gr_operands = -(gr_operand % ',');
gr_identifier = lexeme [ alpha >> *(alnum | '_') ];
gr_operand = gr_identifier | gr_string;
gr_string = lexeme [ '"' >> *("\"\"" | ~char_("\"")) >> '"' ];
gr_newline = +(char_('\r')
|char_('\n')
);
gr_label = gr_identifier >> ':' > gr_newline;
#if 1
on_error<fail>(function, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
on_error<fail>(start, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
on_error<fail>(gr_instruction, phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3));
#else
auto ll = phx::bind(phx::ref(err_handler), L"Error: Expecting ", _4, _3);
on_error<fail>(function, ll);
on_error<fail>(start, ll);
on_error<fail>(gr_instruction, ll);
#endif
// more on_error<fail>s...
BOOST_SPIRIT_DEBUG_NODES((start)(function)(gr_instruction)(gr_operands)(gr_identifier)(gr_operand)(gr_string));
}
my_error_handler<> err_handler;
private:
qi::symbols<char, qi::unused_type> gr_instruction_names;
qi::rule<It, Skipper> start, function, gr_instruction, gr_operands, gr_operand, gr_string;
qi::rule<It, qi::unused_type()> gr_newline;
qi::rule<It, asmast::label(), Skipper> gr_label, gr_identifier;
};
int main()
{
typedef boost::spirit::istream_iterator It;
std::cin.unsetf(std::ios::skipws);
It f(std::cin), l;
parser<It, qi::blank_type> p;
try
{
bool ok = qi::phrase_parse(f,l,p,qi::blank);
if (ok) std::cout << "parse success\n";
else std::cerr << "parse failed: '" << std::string(f,l) << "'\n";
if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";
std::cout << "The 'proof' in the err_handler instance is: " << p.err_handler.proof << "\n";
return ok;
} catch(const qi::expectation_failure<It>& e)
{
std::string frag(e.first, e.last);
std::cerr << e.what() << "'" << frag << "'\n";
}
return false;
}
當喂入輸入
Func Ident{
Mov name, "hello"
Push 5
Exit
}
它打印(作爲第一/最後一個行)
my_error_handler invoked 0
my_error_handler invoked 1
...
The 'proof' in the err_handler instance is: 2
[1]這不是我在想象相關的代碼在第一時間
我剛剛意識到'PHX ::函數'是這是你的'phx :: function &>'思想的明顯替代。看到我的新答案。 –
sehe