Boost Spirit and C++ 11

It's a little trick again to know about. This time it's about Boost Spirit and C++ 11. Boost Spirit is a C++ library to generate a parser, writing directly C++ code, unlike other parser generators like yacc, bison or ANTLR which have their own language. It's cool, very templated and produce fast code. And fun to use !

 If you want to compile the examples in the tutorial (like this one: with C++ 11, you will have a BIG error message, like hundreds of undecipherable lines of templates instantiation errors that are absolutely impossible to read if you're not a world expert, basically the author of the Spirit library.

First time I tried, I ended up with 100+ Kb of error messages ! I tried with different versions of my GCC compiler and clang++ from the LLVM project without any success.

After googling a lot, I finally found a nice little post describing the simple solution to make it work. At the beginning of your code, like the mini_xml3.cpp example, add this, before your #include statements:


The first line will force the Boost libraries to use the C++11 decltype instead of the Boost result_of and the second line will ask Phoenix (another Boost library used by Spirit) to use the latest version V3 instead of the old V2 which is not C++ 11 friendly.

My compiler is gcc 4.8.2 and I use Boost 1.55. Your results might vary with different versions.

By the way, using Phoenix V3 is also needed if you intend to put lambda functions in the action part of your grammar rules (<- i=""> if you understand this function, it means you already read all the Boost Spirit tutorial
). So this trick solves 2 problems.



Popular posts from this blog

log-sum-exp trick

Linear Algebra in C++