Skip to content

Instantly share code, notes, and snippets.

View njlr's full-sized avatar
🌏
F# ing

njlr njlr

🌏
F# ing
View GitHub Profile
template<class L, class R>
auto fold(L l, R r) {
using lTag = typename L::tag;
using rTag = typename R::tag;
if constexpr (is_base_of<rTag, BarTag>::value) {
if constexpr (is_same<lTag, FooTag>::value) {
return foldFB(l, r);
} else {
return foldBB(l, r);
}
template<unsigned n>
struct Fibonacci {
static const unsigned value = Fibonacci<n - 1>::value + Fibonacci<n - 2>::value;
};
template<>
struct Fibonacci<0> {
static const unsigned value = 0;
};
constexpr unsigned fibonacci(const unsigned x) {
return x <= 1 ?
1 :
fibonacci(x - 1) + fibonacci(x - 2);
}
int main() {
return fibonacci(5);
}
import com.google.common.base.Charsets;
import com.google.common.io.MoreFiles;
import com.google.common.io.ByteSink;
import java.nio.file.Path;
public final class GuavaFileUtils {
private GuavaFileUtils() {
}
// Create a vector of 10 elements, all initialized to 0
auto v = Vector<10>(0);
// Set the 1st element to 7
v[0] = 7;
// Set the 9th element to 3
v[8] = 3;
// Print the sum of all elements
template<int D>
struct Vector {
static constexpr unsigned N = D;
int data[N];
Vector(Vector<D> const& v) {
for (int i = 0; i < N; ++i) {
data[i] = v[i];
}
}
#define CREATE_VECTOR(D) \
struct Vector_##D {\
static constexpr int N = D;\
int data[N];\
\
Vector_##D (Vector_##D const& v) {\
for(int i=0; i<N ; ++i) {\
data[i] = v[i];\
}\
}\
CXX Macros Preprocessed Macros Templates
clang 4.0 40s 37s 30s
g++ 6.2 61s 66s 48s
// Vector<16> is in another binary
extern template class Vector<16>;
#include <vector.hpp>
// Instantiation of common sizes
template class Vector<16>;
template class Vector<32>;
template class Vector<64>;
template class Vector<128>;