Skip to content

Instantly share code, notes, and snippets.

@jefftrull
jefftrull / gist:983d783cf508249e5352de8e844a27cb
Created September 20, 2016 05:36
Interesting (possibly wrong) synthesized attribute behavior
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/optional.hpp>
#include <boost/optional/optional_io.hpp>
int main() {
using namespace std;
// parsing parentheses that may or may not contain integers
string in("(10) () (20)");
@jefftrull
jefftrull / gist:0d7ecfef151c39885a7633d7547df8e6
Created April 18, 2016 16:06
*qi::string is compatible with both std::string (concatenation) and std::vector<std::string> (push_back) attributes
#include <string>
#include <vector>
#include <boost/spirit/include/qi.hpp>
using string_it_t = std::string::const_iterator;
int main() {
using namespace boost::spirit;
using namespace boost::spirit::qi;
@jefftrull
jefftrull / gist:2fbbd52a0dec25c1e14d1e69af3361ac
Created April 18, 2016 03:21
Spirit sequence-of-sequences: struct vs. string
// To see if a rule whose attribute is sequence-of-sequence can make appends happen somehow
#include <string>
#include <vector>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
struct tiny {
std::string thing;
@jefftrull
jefftrull / test_ub.cpp
Created March 5, 2016 00:14
Simple test of signed overflow UB
#include <iostream>
#include <cstdint>
#include <limits>
int main(int argc, char **argv) {
std::int32_t i = std::numeric_limits<int32_t>::max();
i++; // signed overflow
if (i < 0) {
std::cerr << "i<0 and i=" << i << "\n"; // "expected" or "safe" result (-O1)
} else {
@jefftrull
jefftrull / as_string.cpp
Created May 18, 2015 19:58
Demonstration of apparent need for as_string when synthesizing a sequence of characters to a std::string
#include <string>
#include <iostream>
#include <sstream>
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
int main() {
@jefftrull
jefftrull / gist:928eac7d47e61f5d9634
Created April 11, 2015 21:09
Demonstration of using a Qi symbol table with a Lex-based Spirit parser, case-insensitive
#include <string>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/lex_lexertl_position_token.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/algorithm/string/case_conv.hpp>
@jefftrull
jefftrull / gist:40a441bdb75365b2bf75
Last active August 29, 2015 14:18
An attempt to construct a functional-style function value-returning function from a mutating function, using Phoenix
// test transforming a mutating function into a value-returning function via Phoenix
// i.e., void f(T& a) -> T f(T const& a) or T F(T a)
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <iostream>
#include <string>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/algorithm/string/case_conv.hpp>
@jefftrull
jefftrull / gist:25fba71dae683bc48f74
Created February 17, 2015 06:58
Demo of getting a dense vector from a row of a sparse matrix in Eigen
#include <iostream>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/Sparse>
int main() {
using namespace std;
using namespace Eigen;
vector<Triplet<double> > tripletList;
@jefftrull
jefftrull / gist:561b4e51d03bb3df3f86
Created December 4, 2014 10:30
attempt to partially specialize class by defining a member function template to be different for a certain *class* template parameters
#include <typeinfo>
#include <string>
#include <iostream>
template<typename T> struct A {
template<typename U>
A(U u) {
std::cout << std::string("class template parameter generic, ctor template parameter ") + typeid(u).name() << std::endl;
}
};
@jefftrull
jefftrull / gist:131626a0dc13238510c2
Created December 3, 2014 18:45
demo "partial" specialization of a class template with a member function template
#include <typeinfo>
#include <string>
#include <iostream>
template<typename T> struct A {
template<typename U>
void foo(U u) {
std::cout << std::string("class template parameter generic, function template parameter ") + typeid(u).name() << std::endl;
}
};