use*_*262 4 c++ boost boost-spirit c++11
请考虑以下代码:(Boost.Spirit 2.5.1)
qi::parse(str.begin(), str.end(), (+qi::alpha)[[](const string& s){cout << s<< '\n';}]
>> (*(qi::char_(',') | qi::char_('\'')))
>> qi::uint_[[](int integer){cout << integer << '\n';}]);
Run Code Online (Sandbox Code Playgroud)
该[[](int integer){cout << integer << '\n';}]作品,但类似的代码+qi::alpha没有.
我该如何更正代码?
的C++ 0x/C++ 11个lambda表达式尚未通过升压精神支持1
编辑显然支持改进(我在今天早些时候的旧版本提升测试).现在使用Boost 1_48和g ++ 4.6.1,just_work出现以下内容.好极了!
qi::as_string[]我想要知道如何qi::as_string获取exposed属性std::string而不是默认属性
std::vector<CharT>:
qi::parse(str.begin(), str.end(),
qi::as_string [ +qi::alpha ]
[
[](const std::string&){std::cout << s << '\n';}
]
>> (*(qi::char_(',') | qi::char_('\'')))
>> qi::uint_
[
[](int integer){std::cout << integer << '\n';}
]);
Run Code Online (Sandbox Code Playgroud)
但在我看来,语法不是很友好.相反,我更喜欢phoenixV2:
qi::parse(str.begin(), str.end(),
qi::as_string [ +qi::alpha ] [ std::cout << _1 << '\n' ]
>> (*(qi::char_(',') | qi::char_('\'')))
>> qi::uint_ [ std::cout << _1 << '\n' ]);
Run Code Online (Sandbox Code Playgroud)
这已经短得多,语法也不那么神秘.看一下
一个有点人为的例子展示了其中的一些实际操作,PLUS将属性引用直接传递给parse函数:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <algorithm>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
bool reverse(std::string& value)
{
std::reverse(value.begin(), value.end());
return true; // false makes the parsing fail
}
int main()
{
std::string str("sometext,123");
auto f(str.begin()), l(str.end());
std::string s;
unsigned int i;
qi::parse(f,l,
qi::as_string [ +qi::alpha ] [ reverse ]
>> qi::omit [ *qi::char_(",\'") ]
>> qi::uint_,
s, i);
std::cout << s << std::endl;
std::cout << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
txetemos
123
Run Code Online (Sandbox Code Playgroud)
1 PhoenixV3可能在1_48中有一些支持(未检查); 你想要的
#define BOOST_RESULT_OF_USE_DECLTYPE
#define BOOST_SPIRIT_USE_PHOENIX_V3
Run Code Online (Sandbox Code Playgroud)
参见例如http://boost.2283326.n4.nabble.com/Boost-Spirit-Phoenix-V3-An-Overview-td3583792.html
您的代码给了我以下编译器错误,跳过一些位
/usr/include/boost/spirit/home/support/action_dispatch.hpp:142:13:
error: no matching function for call to
‘action_dispatch<>::do_call(
const main()::<lambda(const string&)>&,
action_dispatch<>::fwd_tag<std::vector<char>>,
...
Run Code Online (Sandbox Code Playgroud)
归结为传递给你的lambda的论点是vector<char>,而不是string
所以,替换string为vector<char>:
(+qi::alpha)[[](const std::vector<char>& s) {
cout << std::string(s.begin(), s.end()) << '\n';
}]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1827 次 |
| 最近记录: |