Last active
August 29, 2015 14:26
-
-
Save Fiona-J-W/c6c9dff69385cb4cfb2f to your computer and use it in GitHub Desktop.
meta-reduce
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> | |
#include <type_traits> | |
template<template<class, class> class Fun, class Head, class... Tail> | |
struct reduce: Fun<Head, reduce<Fun, Tail...>> {}; | |
template<template<class, class> class Fun, class Head> | |
struct reduce<Fun, Head>: Head{}; | |
template<class I1, class I2> | |
struct plus: std::integral_constant<int, I1::value+I2::value>{}; | |
template<template<class, class> class Fun, int...Args> | |
struct int_reduce: reduce<Fun, std::integral_constant<int, Args>...>{}; | |
int main() { | |
std::cout << int_reduce<plus, 2, 4, -3, 7, 9, 11>::value << '\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment