Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
TupleOfFunctions
#include <iostream> #include <utility> template <typename T, typename U> struct PairOfFunctions: std::pair<T, U> { using std::pair<T, U>::pair; template <typename... Args> auto operator() (Args&&... args) { return std::pair<T, U>::second(std::pair<T, U>::first(args...)); } }; template<typename...> struct TupleOfFunctions; template<typename T, typename... U> struct TupleOfFunctions<T, U...>: PairOfFunctions<T, TupleOfFunctions<U...> >{ using PairOfFunctions<T, TupleOfFunctions<U...> >::PairOfFunctions; TupleOfFunctions(T t, U... u):PairOfFunctions<T, TupleOfFunctions<U...> >(t, TupleOfFunctions<U...>(u...)){} }; template<> struct TupleOfFunctions<>{ template <typename T> T operator() (T t) { // not sure if this is optimal return t; } }; // My universal helper template<template <typename...> class T, typename... Args> auto mk(Args... args){ return T<Args...>{args...}; } int main() { auto a = mk<TupleOfFunctions>( [](int a) {return a*2;}, [](int a) {return a + 10;}, [](int a) {return a / 2;} ); std::cout << a(4) << '\n'; // (4 * 2 + 10) / 2 }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
003#
Lex cpp
Splitwise Problem - 2
2018JanSilv2
C++ Solar eclipse program(Shoushi integrated) 1568 - 1644 also check Lunar eclipse for Xing Yunlu
single_digit
max_size()_30-Seconds-of-C++
CPP Multi Inherit
void sun()
Newspaper
stackse - search stackoverflow differently
Please log in to post a comment.