diff --git a/NextGen.cpp b/NextGen.cpp index 3a2484c..1c68d0e 100644 --- a/NextGen.cpp +++ b/NextGen.cpp @@ -139,6 +139,29 @@ struct CallbackResultDecorator { } }; +template +auto applyTuple(std::integer_sequence, T&& tuple, F&& function) { + return std::forward(function)(std::get(std::forward(tuple))...); +} + +template +struct CallbackResultDecorator> { + template + static auto decorate(Callback&& callback) { + return [callback = std::forward(callback)](auto&&... args) { + // Receive the tuple from the callback + auto result = callback(std::forward(args)...); + return [result = std::move(result)] (auto&& callback) mutable { + // Generate a sequence for tag dispatching + auto constexpr const sequence + = std::make_integer_sequence{}; + applyTuple(sequence, std::move(result), + std::forward(callback)); + }; + }; + } +}; + // Create the proxy callback that is responsible for invoking // the real callback and passing the next continuation into // to the result of the callback. @@ -183,9 +206,11 @@ int main(int, char**) { auto then1 = [](std::string) { int i = 0; + + return std::make_tuple(47, 46, 45); }; - auto then2 = []() { + auto then2 = [](int val1, int val2, int val3) { int i = 0; };