Skip to content

Instantly share code, notes, and snippets.

@shomah4a
Created April 27, 2012 05:05
Show Gist options
  • Save shomah4a/2506033 to your computer and use it in GitHub Desktop.
Save shomah4a/2506033 to your computer and use it in GitHub Desktop.
なにこれたのしいきもい
#include <iostream>
template <typename T>
class Operator
{
public:
template <T left, T right>
class Add
{
public:
static const T result = left + right;
};
template <T left, T right>
class Sub
{
public:
static const T result = left - right;
};
template <T left, T right>
class Mul
{
public:
static const T result = left * right;
};
template <template <T, T> class Op, T... values>
class Fold;
template <template <T, T> class Op, T head, T... left>
class Fold<Op, head, left...>
{
public:
static const T result = Op<head, Fold<Op, left...>::result>::result;
};
template <template <T, T> class Op, T head>
class Fold<Op, head>
{
public:
static const T result = head;
};
};
int main(const int argc, const char* const args[])
{
typedef Operator<int> IntOp;
std::cout << IntOp::Fold<IntOp::Add, 10, 20, 30>::result << std::endl;
std::cout << IntOp::Fold<IntOp::Mul, 10, 20, 30>::result << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment