ä»æ¥ id:uskz ã«ä¼ã£ãã¨ãã«ãC++ã§ãMaybeã¢ããã§ãã¾ãããã£ã¦è¨ãããã®ã§èª¿ã¹ãããããªã®ãã£ã
#include <boost/variant.hpp> class Nothing {}; template <class T> class Just { public: T val; Just(T x) : val(x) {} }; template<class T, class F> boost::variant<Nothing, Just<T> > operator>>=(boost::variant<Nothing, Just<T> > x, F f) { return (boost::get<Nothing>(&x)==NULL) ? f(boost::get<Just<T> >(x).val) : Nothing(); } template <class T> boost::variant<Nothing, Just<T> > Return(T x) { return Just<T>(x); } boost::variant<Nothing, Just<int> > inc(int x) { return Return<int>(x+1); } int main() { boost::variant<Nothing, Just<int> > foo,bar; foo = Just<int>(9); (foo >>= inc) >>= inc; bar = Nothing(); (bar >>= inc) >>= inc; }
ããã§ä½ãä½ããããªã¼ã
â»è¿½è¨ï¼
Maybeã¢ããã¨ããã®ã¯ãç°¡åã«è¨ãã°ã失æãããããããªãå¦çãé£ç¶ãã¦æ¸ãã失æããããã®å¾ã®å¦çã¯ããªããã¨ãããã®ã§ãã
ããæ¸ãããã¯
if (!A) return; if (!B) return; if (!C) return;
ããæ¸ãããã¨ããåæ©
A >>= B >>= C;