【发布时间】:2010-02-07 19:59:30
【问题描述】:
我无法弄清楚我的代码有什么问题。 Boost 的模板让我发疯了!我无法从这一切中得出正面或反面,所以我只好问了。
这是怎么回事?
#include <iostream>
#include <boost/lambda/lambda.hpp>
#include <boost/spirit/include/qi.hpp>
void parsePathTest(const std::string &path)
{
namespace lambda = boost::lambda;
using namespace boost::spirit;
const std::string permitted = "._\\-#@a-zA-Z0-9";
const std::string physicalPermitted = permitted + "/\\\\";
const std::string archivedPermitted = permitted + ":{}";
std::string physical,archived;
// avoids non-const reference to rvalue
std::string::const_iterator begin = path.begin(),end = path.end();
// splits a string like "some/nice-path/while_checking:permitted#symbols.bin"
// as physical = "some/nice-path/while_checking"
// and archived = "permitted#symbols.bin" (if this portion exists)
// I could barely find out the type for this expression
auto expr
= ( +char_(physicalPermitted) ) [lambda::var(physical) = lambda::_1]
>> -(
':'
>> (
+char_(archivedPermitted) [lambda::var(archived) = lambda::_1]
)
)
;
// the error occurs in a template instantiated from here
qi::parse(begin,end,expr);
std::cout << physical << '\n' << archived << '\n';
}
错误的数量是巨大的;我会建议那些想在他们的 on 上帮助编译这个的人(相信我,在这里粘贴是不切实际的)。我正在使用最新的 TDM-GCC 版本 (GCC 4.4.1) 和 Boost 版本 1.39.00。
作为奖励,我想问另外两件事:C++0x 的新 static_assert 功能是否会在这个意义上帮助 Boost,以及我上面引用的实现是否是一个好主意,或者我是否应该使用 Boost 的字符串算法库。后者可能会提供更好的性能吗?
谢谢。
--编辑
以下非常小的示例首先失败,错误与上面的代码完全相同。
#include <iostream>
#include <boost/spirit/include/qi.hpp>
int main()
{
using namespace boost::spirit;
std::string str = "sample";
std::string::const_iterator begin(str.begin()), end(str.end());
auto expr
= ( +char_("a-zA-Z") )
;
// the error occurs in a template instantiated from here
if (qi::parse(begin,end,expr))
{
std::cout << "[+] Parsed!\n";
}
else
{
std::cout << "[-] Parsing failed.\n";
}
return 0;
}
-- 编辑 2
我仍然不知道为什么它在我的旧版本 Boost (1.39) 中不起作用,但升级到 Boost 1.42 解决了这个问题。以下代码在 Boost 1.42 下编译和运行完美:
#include <iostream>
#include <boost/spirit/include/qi.hpp>
int main()
{
using namespace boost::spirit;
std::string str = "sample";
std::string::const_iterator begin(str.begin()), end(str.end());
auto expr
= ( +qi::char_("a-zA-Z") ) // notice this line; char_ is not part of
// boost::spirit anymore (or maybe I didn't
// include the right headers, but, regardless,
// khaiser said I should use qi::char_, so here
// it goes)
;
// the error occurs in a template instantiated from here
if (qi::parse(begin,end,expr))
{
std::cout << "[+] Parsed!\n";
}
else
{
std::cout << "[-] Parsing failed.\n";
}
return 0;
}
感谢 hkaiser 的提示。
【问题讨论】:
-
出现前几个错误仍然会有所帮助。此外,更喜欢使用预先存在的库。它们附带免费的错误修复,并且已经过测试。
-
GMan:你认为 boost 库是由 n2liquid 以某种方式自制的吗?也许你应该自己看看 boost.org!
-
@Pontus:什么?也许我误解了,但我不知道你是如何从我的评论中得到的。我非常清楚 Boost 是什么。
-
@GMan 那么用什么预先存在的库替换什么是什么意思?我也真的不明白。
-
"以及我上面引用的实现是否是个好主意,或者我是否应该使用 Boost 的字符串算法库。"也许我不明白,但据我所知,您是在问是否使用 Bosot.String 库或您自己手工制作的东西。这是不正确的吗?
标签: c++ c++11 boost-spirit boost-spirit-qi