This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <utility> | |
#include <tuple> | |
#include <future> | |
#include <mutex> | |
#include <thread> | |
#include <iostream> | |
// This code is used to unpack tuple elements into function arguments. | |
// apply(f, tuple<int,bool,char>) would be unpacked as f(int,bool,char) | |
// This implementation has been specialized for tuples of futures. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <utility> | |
#include <tuple> | |
#include <future> | |
#include <mutex> | |
#include <thread> | |
#include <iostream> | |
// This code is used to unpack tuple elements into function arguments. | |
// apply(f, tuple<int,bool,char>) would be unpacked as f(int,bool,char) | |
// This implementation has been specialized for tuples of futures. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T, typename Hasher = ::std::hash<T>> | |
class hash_ptr { | |
public: | |
using value_type = T; | |
using reference = std::add_lvalue_reference_t<value_type>; | |
using const_reference = const value_type &; | |
using pointer = T*; | |
using hash_type = typename Hasher::result_type; | |
//using hash_type = typename decltype( std::declval<Hasher>( )( std::declval<value_type>( ) ) ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// All content copyright (C) Allan Deutsch 2017. All rights reserved. | |
#pragma once | |
#include <cstdint> | |
// According to the top post here: http://softwareengineering.stackexchange.com/a/145633 | |
// murmur2 is the best hash for speed and random distribution. | |
// This is a constexpr implementation of Murmur2A, a hash algorithm designed by Austin Appleby. | |
// This version is optimized for 64bit architectures. | |
constexpr uint64_t murmur2a_64_ct( const char *str, unsigned long size, uint64_t seed ) { | |
constexpr uint64_t prime{ 0xc6a4a7935bd1e995ull }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
template<typename Ret, typename... Args> | |
constexpr int arg_count(Ret(*)(Args...)){ | |
return sizeof...(Args); | |
} | |
void foo(int, float, double, char){} | |
int foo2(float, int, char*){return 0;} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is my implementation submission for this page: http://www.programming-idioms.org/idiom/36/first-class-function-generic-composition | |
template<typename Pred1, typename Pred2> | |
auto compose(Pred1 f, Pred2 g){ | |
return [f,g](auto x){ return g(f(x)); }; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file is to test the behavior of various compilers' return value optimizations (RVO) in different scenarios | |
#include <iostream> | |
#include <cstdlib> // std::rand | |
#include <array> | |
#include <utility> // std::move | |
// This class is small enough to fit in a register, and should be a simple target for return value optimization | |
class SmallObject { | |
public: | |
static unsigned instance_count; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <ostream> | |
template<typename... Predicates> | |
struct overload; | |
template<typename Pred, typename... Predicates> | |
struct overload<Pred, Predicates...> : Pred, overload<Predicates...> | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <type_traits> | |
// Create our interface | |
// usage: T1 is the typename, x1 is the variable name. | |
#define DEFINE_SFINAE_CHECK( NAME, EXPR ) \ | |
template <typename U1> \ | |
struct NAME \ | |
{ \ | |
using yes = char; \ | |
using no = char[2]; \ | |
template <typename T1>\ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cassert> | |
#include <cmath> | |
int main() | |
{ | |
auto add = [](auto x) { return [x](auto y) { return x + y; }; }; | |
auto pyth = [](auto x) { return [x](auto y) { return std::sqrt(x * x + y * y); }; }; | |
// are lambdas really fucking dumb?!?! | |
auto add4 = add(4); |