X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FUtility.h;h=4d6725d7070ec70e9684d19db915100c6fc5e9cc;hb=55791684950381d92cef305d1e9124120b178ad5;hp=385ed34ac7d52b5329df8dc59aecc23400b4c4c9;hpb=3cec19d4760d9b4057c7e4a6464be86aad216a73;p=folly.git diff --git a/folly/Utility.h b/folly/Utility.h index 385ed34a..4d6725d7 100644 --- a/folly/Utility.h +++ b/folly/Utility.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include @@ -83,7 +84,7 @@ constexpr typename std::decay::type copy(T&& value) noexcept( */ #if __cpp_lib_as_const || _MSC_VER -/* using override */ using std::as_const +/* using override */ using std::as_const; #else @@ -96,4 +97,65 @@ template void as_const(T const&&) = delete; #endif + +#if __cpp_lib_integer_sequence || _MSC_VER + +/* using override */ using std::integer_sequence; +/* using override */ using std::index_sequence; +/* using override */ using std::make_index_sequence; + +#else + +template +struct integer_sequence { + using value_type = T; + + static constexpr std::size_t size() noexcept { + return sizeof...(Ints); + } +}; + +template +using index_sequence = folly::integer_sequence; + +namespace detail { +template +struct make_index_sequence + : detail::make_index_sequence {}; + +template +struct make_index_sequence<0, Ints...> : folly::index_sequence {}; +} + +template +using make_index_sequence = detail::make_index_sequence; + +#endif + +/** + * A simple function object that passes its argument through unchanged. + * + * Example: + * + * int i = 42; + * int &j = Identity()(i); + * assert(&i == &j); + * + * Warning: passing a prvalue through Identity turns it into an xvalue, + * which can effect whether lifetime extension occurs or not. For instance: + * + * auto&& x = std::make_unique(42); + * cout << *x ; // OK, x refers to a valid unique_ptr. + * + * auto&& y = Identity()(std::make_unique(42)); + * cout << *y ; // ERROR: y did not lifetime-extend the unique_ptr. It + * // is no longer valid + */ +struct Identity { + using is_transparent = void; + template + constexpr T&& operator()(T&& x) const noexcept { + return static_cast(x); + } +}; }