Skip to content

Instantly share code, notes, and snippets.

@nico159
Created June 11, 2013 21:54
Show Gist options
  • Save nico159/5761079 to your computer and use it in GitHub Desktop.
Save nico159/5761079 to your computer and use it in GitHub Desktop.
Testing C++ metaprogramming
#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