Skip to content

Instantly share code, notes, and snippets.

@Fiona-J-W
Last active August 29, 2015 14:26
Show Gist options
  • Save Fiona-J-W/c6c9dff69385cb4cfb2f to your computer and use it in GitHub Desktop.
Save Fiona-J-W/c6c9dff69385cb4cfb2f to your computer and use it in GitHub Desktop.
meta-reduce
#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