Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Expected types
//Title of this code #include <iostream> #include <stdexcept> #include <exception> using namespace std; template<typename T> class Expected { private: union { T t_; std::exception_ptr e_; }; bool bGot_; Expected() : bGot_(false) { } public: Expected(const T &t) : t_(t), bGot_(true) { std::cout<<"Expected(const T &)\n"; } Expected(T &&t) : t_(std::move(t)), bGot_(true) { std::cout<<"Expected(T &&)\n"; } Expected(const Expected<T> &other) : bGot_(other.bGot_) { if (bGot_) { new (&t_)T(other.t_); } else { new (&e_)std::exception_ptr(other.e_); } } Expected(Expected<T> &&other) : bGot_(other.bGot_) { if (bGot_) { new (&t_)T(std::move(other.t_)); } else { new (&e_)std::exception_ptr(std::move(other.e_)); } } ~Expected() { } bool valid() const { return bGot_; } template<typename E> bool hasException() const { if (!valid()) { try { std::rethrow_exception(e_); } catch(const E &e) { return true; } catch(...) { } } return false; } T & get() { if (!valid()) { std::rethrow_exception(e_); } return t_; } const T & get() const { if (!valid()) { std::rethrow_exception(e_); } return t_; } static Expected<T> fromException(std::exception_ptr ptr) { Expected<T> result; new (&result.e_)std::exception_ptr(std::move(ptr)); return result; } template<typename E> static Expected<T> fromException(const E &ex) { return fromException(std::make_exception_ptr(ex)); } static Expected<T> fromException() { return fromException(std::current_exception()); } template<typename F> static Expected<T> fromCode(F fun) { try { return Expected<T>(fun()); } catch(...) { return fromException(); } } }; class Obj { private: int p_; public: Obj() { std::cout<<"Obj()\n"; } Obj(const Obj &other) : p_(other.p_) { std::cout<<"Obj copy()\n"; } Obj(Obj &&other) : p_(std::move(other.p_)) { std::cout<<"Obj move\n"; } Obj & operator = (const Obj &other) { p_ = other.p_; std::cout<<"Obj =\n"; return *this; } Obj(int p) : p_(p) { std::cout<<"Obj(int)\n"; } ~Obj() { std::cout<<"~Obj\n"; } int getValue() const { return p_; } }; Expected<Obj> fun() { std::cout<<"fun\n"; return Expected<Obj>::fromException(std::invalid_argument("bad number!")); return Expected<Obj>::fromException(std::runtime_error("bad argument!")); return Obj(3); } int fun2() { return 8; } int main() { auto e = fun(); if (e.valid()) { std::cout<<e.get().getValue()<<'\n'; } else { std::cout<<"error\n"; if (e.hasException<std::invalid_argument>()) { std::cout<<"invalid argument\n"; } else if (e.hasException<std::runtime_error>()) { std::cout<<"runtime error!\n"; } else { std::cout<<"unknown exception!\n"; } } auto f = Expected<int>::fromCode( []() { return fun2(); }); std::cout<<f.get()<<'\n'; std::cout<<e.get().getValue()<<'\n'; }
run
|
edit
|
history
|
help
0
Dar
ExceptionHandling4
Einstein's Physics
Splitwise Problem - 1
ThreeVariableRegression
Heap DS and Heapsort
Median of two sorted arrays
Making pyramid using nested loop 1/2
sysTest.cpp
Height of a binary tree