Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
TupleOfFunctions
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#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 }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.64 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 4 Mb, absolute service time: 0.79 sec
edit mode
|
history
|
discussion
9