-
-
Save nico159/5761079 to your computer and use it in GitHub Desktop.
Testing C++ metaprogramming
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 T> | |
struct Identity | |
{ | |
typedef T result; | |
}; | |
template <typename T, T v> | |
struct Constant | |
{ | |
typedef T type; | |
static constexpr T value = v; | |
}; | |
template <int N> | |
using Int = Constant<int, N>; | |
template <template<typename, typename> class fun, typename arg1> | |
struct bind | |
{ | |
template <typename arg2> | |
struct result1 : fun<arg1, arg2> | |
{}; | |
}; | |
template <typename left, typename right> | |
struct plus : Identity<Constant<decltype(left::value + right::value), left::value + right::value>> | |
{}; | |
template <typename arg> | |
struct plus1 : bind<plus, Int<1>>::result1<arg> | |
{}; | |
int main() | |
{ | |
std::cout << plus1<Int<100>>::result1::result::value << std::endl; | |
std::cout << Int<101>::value << std::endl; | |
std::cout << std::boolalpha << std::is_same<plus1<Int<100>>::result1::result, Int<101>>::value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment