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
// Type Erasure | |
// https://channel9.msdn.com/Events/GoingNative/2013/Inheritance-Is-The-Base-Class-of-Evil | |
// https://akrzemi1.wordpress.com/2013/11/18/type-erasure-part-i/ | |
#include<memory> | |
#include<iostream> | |
struct Printable { | |
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<vector> | |
#include<string> | |
#include<set> | |
auto numberOfUniqueChars(std::string const& str) { | |
std::set<char> s; | |
for(auto c : str) { | |
s.insert(c); | |
} |
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<vector> | |
#include<iostream> | |
template<class T> | |
struct Matrix { | |
Matrix(std::size_t n=1, std::size_t m=1) | |
: n{n}, m{m}, data(n*m) | |
{} | |
Matrix(std::size_t n, std::size_t m, std::vector<T> const& data) |
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<functional> | |
template<class T> | |
struct AsFunction | |
: public AsFunction<decltype(&T::operator())> | |
{}; | |
template<class ReturnType, class... Args> | |
struct AsFunction<ReturnType(Args...)> { | |
using type = std::function<ReturnType(Args...)>; |
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<functional> | |
template<class T> | |
struct AsFunction | |
: public AsFunction<decltype(&T::operator())> | |
{}; | |
template<class ReturnType, class... Args> | |
struct AsFunction<ReturnType(Args...)> { | |
using type = std::function<ReturnType(Args...)>; |
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
// Example program | |
#include <iostream> | |
#include <string> | |
struct Dual { | |
float real; | |
float img; | |
Dual(float x, float y=0) | |
:real{x}, img{y} |
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> | |
template<class L> | |
struct Left { | |
L const value; | |
}; | |
template<class R> | |
struct Right { | |
R const value; |
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<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]; | |
} | |
} |
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
#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];\ | |
}\ |