From ac9a0f0dbfeb8b76207202dcda2032faff6ad541 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 23 Jan 2017 13:56:43 -0800 Subject: [PATCH] Rework iothread_perform for void return types Need to use a template specialization so we don't try to create a variable of type void --- src/iothread.h | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/iothread.h b/src/iothread.h index 5faa477e7..41520208c 100644 --- a/src/iothread.h +++ b/src/iothread.h @@ -29,13 +29,31 @@ void iothread_drain_all(void); int iothread_perform(std::function &&func, std::function &&completion = std::function()); -// Variant that allows computing a value in func, and then passing it to the completion handler +// Template helpers template -int iothread_perform(std::function &&handler, std::function &&completion) { - T *result = new T(); - return iothread_perform([=](){ *result = handler(); }, - [=](){ completion(std::move(*result)); delete result; } - ); +struct _iothread_trampoline { + template + static int perform(const HANDLER &handler, const COMPLETION &completion) { + T *result = new T(); + return iothread_perform([=](){ *result = handler(); }, + [=](){ completion(std::move(*result)); delete result; }); + } +}; + + +// Void specialization +template<> +struct _iothread_trampoline { + template + static int perform(const HANDLER &handler, const COMPLETION &completion) { + return iothread_perform(handler, completion); + } +}; + +// Variant that allows computing a value in func, and then passing it to the completion handler +template +int iothread_perform(const HANDLER &handler, const COMPLETION &completion) { + return _iothread_trampoline::perform(handler, completion); } /// Legacy templates