Skip to content

Instantly share code, notes, and snippets.

clang++ -Xclang -fdump-record-layouts file.cpp
@jbelloncastro
jbelloncastro / generate_array.cpp
Last active July 8, 2019 12:28
Create arrays of no-default-constructible elements with std::generate like syntax.
#include <array>
#include <utility>
template < class F, size_t... Ns >
constexpr
std::array<decltype(std::declval<F>()(size_t(0))),sizeof...(Ns)>
generate_array( F&& f, std::index_sequence<Ns...> )
{
return { f(Ns)... };
}
@jbelloncastro
jbelloncastro / literal.hpp
Last active August 22, 2019 16:15
Embed string literals in a datatype
#include <array>
#include <tuple>
#include <string_view>
#include <utility>
template < class CharT, CharT... cs >
struct Literal {
static bool match( std::basic_string_view<CharT> s ) {
static constexpr std::array<CharT,sizeof...(cs)> value{cs...};
return std::equal(value.begin(), value.end(), s.begin(), s.end());
@jbelloncastro
jbelloncastro / sax_parse.cc
Last active August 28, 2019 09:58
nlohmann::json SAX parsing example
#ifndef SAX_H
#define SAX_H
#include <nlohmann/json.hpp>
#include <bitset>
#include <string_view>
#include <sstream>
template < class T >
struct sax_reader;
@jbelloncastro
jbelloncastro / integer-string-literal.cpp
Created September 9, 2019 10:56
Create a string from an integer without using std::to_string or std::printf
#include <algorithm>
#include <cstring>
#include <string>
#include <limits>
// Literal for the IEC Mebi binary prefix
static
std::string operator""_Mi( unsigned long long value ) {
struct Generator {
Generator( unsigned long long value ) :
@jbelloncastro
jbelloncastro / plot.gnuplot
Created September 30, 2019 17:12
Gnuplot chart for a histogram with error bars
#!/usr/bin/gnuplot
# Output format
set terminal png enhanced font "freesans,16" linewidth 2
# Caption
set key inside top horizontal nobox font ",12"
set xlabel ''
set ylabel ''
set auto x
@jbelloncastro
jbelloncastro / dispatch.cpp
Last active October 4, 2019 17:24
Double dispatch
#include <memory>
#include <tuple>
#include <algorithm>
#include <experimental/array>
#include <type_traits>
namespace {
// This fails to evaluate if any element in Args is not a valid type.
// Otherwise it evaluates to void.
@jbelloncastro
jbelloncastro / tuple_switch.cpp
Last active October 8, 2019 18:03
Switch/case like function for tuples/pairs
#include <tuple>
#include <type_traits>
enum class A {
a, b
};
enum class B {
c, d
};
@jbelloncastro
jbelloncastro / dispatch_functor.cpp
Last active October 23, 2019 14:32
Deduce function argument type and perform runtime type dispatch based on that type
#include <type_traits>
#include <llvm/ADT/STLExtras.h>
struct Base { virtual ~Base() = default; };
bool traverse( const Base* base, llvm::function_ref<bool(const Base*)> );
struct dispatcher {
template <class F, class U, class... Args>
auto operator()( F &&f, U &&u, Args&&... args ) const {
@jbelloncastro
jbelloncastro / ival_overwrite.cpp
Last active October 26, 2019 10:45
Interval map that overwrites values instead of accumulating
#include <boost/icl/interval_map.hpp>
#include <boost/optional.hpp>
#include <iostream>
using namespace boost::icl;
template <class Type>
struct inplace_overwrite : identity_based_inplace_combine<Type> {
void operator()(Type &object, const Type &operand) const { object = operand; }
};