Created
May 10, 2019 19:15
-
-
Save kim366/1cbc8ad8a7a4806f3cdced4a34ee5ae8 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
template<int N, typename Tupl, typename Op> | |
struct tuple_transform_impl | |
{ | |
static void increment(Tupl& tuple, Op op) | |
{ | |
std::get<N>(tuple) = op(std::get<N>(tuple)); | |
tuple_transform_impl<N - 1, Tupl, Op>::increment(tuple, op); | |
} | |
}; | |
template<typename Tupl, typename Op> | |
struct tuple_transform_impl<-1, Tupl, Op> | |
{ | |
static void increment(Tupl&, Op) {} | |
}; | |
template<template<typename...> typename Tupl, typename... Elems, typename Op> | |
void increment_tuple(Tupl<Elems...>& tuple, Op op) | |
{ | |
tuple_transform_impl<sizeof...(Elems) - 1, Tupl<Elems...>, Op>::increment(tuple, op); | |
} | |
int main() | |
{ | |
auto tuple = std::make_tuple(1, 3, 6, 8); | |
auto expected = std::make_tuple(2, 4, 7, 9); | |
increment_tuple(tuple, [] (auto x) { return x + 1; }); | |
assert(tuple == expected); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment