now Function throws on call if decltype(_fun.get()) is std::nullptr_t.

This commit is contained in:
Kjistóf 2016-11-27 14:26:31 +01:00
parent 087f3d45bd
commit a57f5d81ca
1 changed files with 15 additions and 1 deletions

View File

@ -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<callable<Fun>>(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>(args)...); }
{
if (_is_null)
throw std::bad_function_call("Error: call to nullptr Function!");
return _fun->call(std::forward<Args>(args)...);
}
operator bool() const
{ return static_cast<bool>(_fun); }