From a57f5d81ca94704b6c8962b47586258c1cc5aafb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 27 Nov 2016 14:26:31 +0100 Subject: [PATCH] now Function throws on call if decltype(_fun.get()) is std::nullptr_t. --- Function.hpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Function.hpp b/Function.hpp index 47209ea..db94d82 100644 --- a/Function.hpp +++ b/Function.hpp @@ -33,6 +33,12 @@ private: }; std::unique_ptr _fun; + /* kérdés, hogy ezt lehet-e szebben, vagy ez így a state-of-the-art megoldás? + * lent majd látod, de úgy van megoldva, hogy overloadolva van az operator= + * std::nullptr_t-re. próbáltam varázsolni ilyen std::is_null_pointer-rel, + * meg hasonlókkal a callable::operator()-ben, meg a Function::operator()-ben, + * de nem akarta az igazat. */ + bool _is_null = false; public: Function() {} @@ -41,11 +47,19 @@ public: Function& operator=(Fun fun) { _fun = std::make_unique>(fun); + _is_null = false; return *this; } + Function& operator=(std::nullptr_t) + { _is_null = true; } + Ret operator()(Args... args) const - { return _fun->call(std::forward(args)...); } + { + if (_is_null) + throw std::bad_function_call("Error: call to nullptr Function!"); + return _fun->call(std::forward(args)...); + } operator bool() const { return static_cast(_fun); }