Skip to content

Instantly share code, notes, and snippets.

@sekia
Created October 7, 2016 15:13
Show Gist options
  • Select an option

  • Save sekia/f89e1b79cd45f6052b14c9da6fb3a994 to your computer and use it in GitHub Desktop.

Select an option

Save sekia/f89e1b79cd45f6052b14c9da6fb3a994 to your computer and use it in GitHub Desktop.
C++ TMP exercise
template <typename S, typename T, typename F, S Init, T ...Values>
struct Fold {
template <T Car, T ...Cdr>
struct Fold1 {
constexpr static S Init_ = F::template Result<Init, Car>::Value;
constexpr static S Value = Fold<S, T, F, Init_, Cdr...>::Value;
};
constexpr static S Value = Fold1<Values...>::Value;
};
template <typename S, typename T, typename F, S Init>
struct Fold<S, T, F, Init> { constexpr static S Value = Init; };
template <typename First, typename Second>
struct Divide {
template <First A, Second B>
struct Result { constexpr static auto Value = A / B; };
};
template <typename T, T Car, T ...Cdr>
struct Min {
struct Min1 {
template <T A, T B>
struct Result { constexpr static auto Value = A < B ? A : B; };
};
constexpr static T Value = Fold<T, T, Min1, Car, Cdr...>::Value;
};
#include <iostream>
using namespace std;
int main(int, char **) {
cout << Fold<int, int, Divide<int, int>, 78, 2, 3>::Value << endl;
cout << Min<char, 'a', 'b', 'e', 'f', 'D', 'C', 'G'>::Value << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment