Created
April 27, 2012 05:05
-
-
Save shomah4a/2506033 to your computer and use it in GitHub Desktop.
なにこれたのしいきもい
This file contains hidden or 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 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