这里有一个例子也不起作用。使用 Visual Studio 2012 都会发出警告:
boost/iterator/iterator_adaptor.hpp(306): warning C4172: returning address of local variable or temporary
boost_1_52_0\boost/iterator/iterator_adaptor.hpp(306) : while compiling class template member function 'const char &boost::iterator_adaptor<Derived,Base,Value,Traversal>::dereference(void) const'
with
[
Derived=boost::spirit::classic::position_iterator2<forward_iterator_type>,
Base=forward_iterator_type,
Value=const char,
Traversal=boost::forward_traversal_tag
]
快速谷歌搜索“iterator_adaptor dereference temporary”会导致建议Reference
参数iterator_adaptor
为非引用类型。
为了实现这一点,您需要更改文件“boost/spirit/home/classic/iterator/impl/position_iterator.ipp”。具体来说,您需要更改:
typedef boost::iterator_adaptor<
main_iter_t,
ForwardIterT,
const_value_type,
boost::forward_traversal_tag
> type;
到:
typedef boost::iterator_adaptor<
main_iter_t,
ForwardIterT,
const_value_type,
boost::forward_traversal_tag,
const_value_type
> type;
这会导致 g++ 和 vc11 中出现新错误:
boost_1_52_0\boost/concept_check.hpp(212): error C2440: 'initializing' : cannot convert from 'boost::detail::iterator_category_with_traversal<Category,Traversal>' to 'std::forward_iterator_tag'
with
[
Category=std::input_iterator_tag,
Traversal=boost::forward_traversal_tag
]
No constructor could take the source type, or constructor overload resolution was ambiguous
如果将iterator_adaptor
typedef 更改为:
typedef boost::iterator_adaptor<
main_iter_t,
ForwardIterT,
const_value_type,
std::forward_iterator_tag,
const_value_type
> type;
这使得下面的程序(基于您的代码)和 boost-spirit.com 中的示例都可以工作,但我不确定它在其他情况下不会中断,因此请自行决定使用它。
#include <vector>
#include <istream>
#include <sstream>
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
int main()
{
std::string TEST = "aa\nbbbb\nccc\n";
std::istringstream INPUT (TEST);
std::noskipws(INPUT);
typedef std::istreambuf_iterator<char> base_iterator;
typedef boost::spirit::multi_pass<base_iterator> multi_pass_iter;
typedef boost::spirit::classic::position_iterator2<multi_pass_iter> pos_iterator;
base_iterator base_begin(INPUT);
multi_pass_iter first = boost::spirit::make_default_multi_pass(base_begin);
multi_pass_iter last;
pos_iterator pfirst(first,last,std::string("DD"));
pos_iterator plast;
using qi::lexeme;
using ascii::alpha;
std::vector<std::string> DDD;
bool res = qi::phrase_parse(pfirst,plast,* lexeme[+alpha],ascii::space,DDD);
if(res && pfirst==plast)
{
for (const auto & d : DDD)
std::cout << d << " (" << d.size() << ")" << std::endl;
}
else
{
std::cout << "Parsing error." << std::endl;
}
return 0;
}