问题描述
(源自this问题。)
我尝试使用this示例失败。
我的尝试是复制并轻松编辑链接上的代码。结果是这样的(我在Boost网站上留下了评论,以使我的编辑内容与原始内容的比较更加容易):
#include <boost/wave.hpp>
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
int main() {
// The following preprocesses a given input file.
// Open the file and read it into a string variable
std::string input("int main() { std::cout << \"Hello world\"; }");
// The template boost::wave::cpplexer::lex_token<> is the
// token type to be used by the Wave library.
// This token type is one of the central types throughout
// the library,because it is a template parameter to some
// of the public classes and templates and it is returned
// from the iterators.
// The template boost::wave::cpplexer::lex_iterator<> is
// the lexer iterator to use as the token source for the
// preprocessing engine. In this case this is parameterized
// with the token type.
typedef boost::wave::cpplexer::lex_iterator<
boost::wave::cpplexer::lex_token<> >
lex_iterator_type;
typedef boost::wave::context<
std::string::iterator,lex_iterator_type>
context_type;
context_type ctx(input.begin(),input.end(),"preproc.cpp");
// At this point you may want to set the parameters of the
// preprocessing as include paths and/or predefined macros.
//ctx.add_include_path("...");
//ctx.add_macro_deFinition("...");
// Get the preprocessor iterators and use them to generate
// the token sequence.
context_type::iterator_type first = ctx.begin();
context_type::iterator_type last = ctx.end();
// The input stream is preprocessed for you during iteration
// over [first,last)
while (first != last) {
std::cout << (*first).get_value();
++first;
}
}
我可以成功地编译
g++ -std=c++17 -lboost_thread -lboost_filesystem -lboost_wave source.cpp
terminate called after throwing an instance of 'boost::wrapexcept<boost::wave::preprocess_exception>'
what(): boost::wave::preprocess_exception
Aborted (core dumped)
如评论中所建议,将std::cout << (*first).get_value();
更改为std::cout << (*first).get_value() << '\n';
时,很明显在EOF上引发了异常。仅凭猜测,我尝试在\n
的末尾添加input
,程序运行正常,没有任何误解。
但是,当我将代码恢复为链接的示例时,是从文件中读取input
而不是对其进行硬编码,
//std::string input("int main() { std::cout << \"Hello world\"; }");
std::ifstream instream("somefile.cpp");
std::string input(
std::istreambuf_iterator<char>(instream.rdbuf()),std::istreambuf_iterator<char>());
然后我遇到了同样的错误。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)