From 14321daf624b1359825674da0b4b49a51b94784c Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Fri, 21 Aug 2020 23:31:02 +0200 Subject: [PATCH] Improved dispatch. --- src/torch_stablesort/dispatch_test.cpp | 36 ++++++++++---------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/torch_stablesort/dispatch_test.cpp b/src/torch_stablesort/dispatch_test.cpp index 8079998..5756088 100644 --- a/src/torch_stablesort/dispatch_test.cpp +++ b/src/torch_stablesort/dispatch_test.cpp @@ -1,33 +1,25 @@ #include #include -template -class Dispatcher { -public: - void dispatch(int x, Ts&& ...args) { - if (x > 0) - call(std::forward(args)...); - else - call(std::forward(args)...); - } +template class F, typename... Ts> +void dispatch(int x, Ts&& ...args) { + if (x > 0) + F()(std::forward(args)...); + else + F()(std::forward(args)...); +} -protected: - template - void call(Ts&& ...args) { - throw std::runtime_error("Not implemented"); +template +struct bla { + void operator()(int&& a, char&& b, double&& c) const { + std::cout << typeid(T).name() << " " << a << " " << b << " " << c << std::endl; } }; -class bla {}; - -template<> template -void Dispatcher::call(int&& a, char&& b, double&& c) { - std::cout << typeid(T).name() << " " << a << " " << b << " " << c << std::endl; -} main() { std::cout << "main()" << std::endl; - Dispatcher d; - d.dispatch(5, 1, 'a', 5.5); - d.dispatch(-5, 1, 'a', 5.5); + //bla()(1, 'a', 5.5); + dispatch(5, 1, 'a', 5.5); + dispatch(-5, 1, 'a', 5.5); }