From: Yedidya Feldblum Date: Wed, 4 Jan 2017 03:37:13 +0000 (-0800) Subject: folly::_t and use it in folly::exception_wrapper X-Git-Tag: v2017.03.06.00~137 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=815100a0d3a73ff7800c588de288c66d920b0d6c folly::_t and use it in folly::exception_wrapper Summary: [Folly] `folly::_t` and use it in `folly::exception_wrapper`. Intended in part for use in Folly code, but can be used outside. Instead of: ```lang=c++ namespace folly { using original = //... using decayed = typename std::decay::type; } ``` In C++14: ```lang=c++ namespace folly { using original = //... using decayed = std::decay_t; } ``` And in C++11: ```lang=c++ namespace folly { using original = //... using decayed = _t>: } ``` Not perfect, but better. HT ericniebler and https://github.com/eniebler/meta. Reviewed By: ericniebler Differential Revision: D4371539 fbshipit-source-id: 9046d9caab73141b95f4bce4fb1af26e0c1ac739 --- diff --git a/folly/ExceptionWrapper.h b/folly/ExceptionWrapper.h index a6023267..b573f23f 100644 --- a/folly/ExceptionWrapper.h +++ b/folly/ExceptionWrapper.h @@ -25,6 +25,7 @@ #include #include +#include namespace folly { @@ -115,12 +116,11 @@ class exception_wrapper { // Implicitly construct an exception_wrapper from a qualifying exception. // See the optimize struct for details. - template ::type>::value> - ::type> + template < + typename Ex, + typename = _t>>::value>>> /* implicit */ exception_wrapper(Ex&& exn) { - typedef typename std::decay::type DEx; - assign_sptr(std::make_shared(std::forward(exn))); + assign_sptr(std::make_shared<_t>>(std::forward(exn))); } // The following two constructors are meant to emulate the behavior of @@ -195,13 +195,13 @@ class exception_wrapper { template bool with_exception(F&& f) { - using arg_type = typename functor_traits::arg_type_decayed; + using arg_type = _t::arg_type>>; return with_exception(std::forward(f)); } template bool with_exception(F&& f) const { - using arg_type = typename functor_traits::arg_type_decayed; + using arg_type = _t::arg_type>>; return with_exception(std::forward(f)); } @@ -210,13 +210,13 @@ class exception_wrapper { // will otherwise return false. template bool with_exception(F f) { - return with_exception1::type>(f, this); + return with_exception1<_t>>(std::forward(f), this); } // Const overload template bool with_exception(F f) const { - return with_exception1::type>(f, this); + return with_exception1<_t>>(std::forward(f), this); } std::exception_ptr getExceptionPtr() const { @@ -285,10 +285,8 @@ class exception_wrapper { struct impl { using arg_type = A; }; template struct impl { using arg_type = A; }; - using functor_decayed = typename std::decay::type; - using functor_op = decltype(&functor_decayed::operator()); + using functor_op = decltype(&_t>::operator()); using arg_type = typename impl::arg_type; - using arg_type_decayed = typename std::decay::type; }; template @@ -302,16 +300,13 @@ class exception_wrapper { template using is_exception_ = std::is_base_of; - template - using conditional_t_ = typename std::conditional::type; - template - static typename std::enable_if::value, T*>::type + static _t::value, T*>> try_dynamic_cast_exception(F* from) { return dynamic_cast(from); } template - static typename std::enable_if::value, T*>::type + static _t::value, T*>> try_dynamic_cast_exception(F*) { return nullptr; } @@ -321,7 +316,7 @@ class exception_wrapper { // instantiation which works with F. template static bool with_exception1(F f, T* that) { - using CEx = conditional_t_::value, const Ex, Ex>; + using CEx = _t::value, const Ex, Ex>>; if (is_exception_::value && that->item_) { if (auto ex = try_dynamic_cast_exception(that->item_.get())) { f(*ex); @@ -393,19 +388,18 @@ fbstring exceptionStr(const exception_wrapper& ew); namespace try_and_catch_detail { -template -using enable_if_t_ = typename std::enable_if::type; - template using is_wrap_ctor = std::is_constructible; template -inline enable_if_t_::value, exception_wrapper> make(Ex& ex) { +inline _t::value, exception_wrapper>> make( + Ex& ex) { return exception_wrapper(std::current_exception(), ex); } template -inline enable_if_t_::value, exception_wrapper> make(Ex& ex) { +inline _t::value, exception_wrapper>> make( + Ex& ex) { return typeid(Ex&) == typeid(ex) ? exception_wrapper(ex) : exception_wrapper(std::current_exception(), ex); diff --git a/folly/Traits.h b/folly/Traits.h index c58648b1..81f52d0a 100644 --- a/folly/Traits.h +++ b/folly/Traits.h @@ -128,6 +128,27 @@ namespace folly { +/*** + * _t + * + * Instead of: + * + * using decayed = typename std::decay::type; + * + * With the C++14 standard trait aliases, we could use: + * + * using decayed = std::decay_t; + * + * Without them, we could use: + * + * using decayed = _t>; + * + * Also useful for any other library with template types having dependent + * member types named `type`, like the standard trait types. + */ +template +using _t = typename T::type; + /** * IsRelocatable::value describes the ability of moving around * memory a value of type T by using memcpy (as opposed to the