2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author: Andrei Alexandrescu
23 #include <type_traits>
26 #include <folly/Portability.h>
28 // libc++ doesn't provide this header, nor does msvc
29 #ifdef FOLLY_HAVE_BITS_CXXCONFIG_H
30 // This file appears in two locations: inside fbcode and in the
31 // libstdc++ source code (when embedding fbstring as std::string).
32 // To aid in this schizophrenic use, two macros are defined in
34 // _LIBSTDCXX_FBSTRING - Set inside libstdc++. This is useful to
35 // gate use inside fbcode v. libstdc++
36 #include <bits/c++config.h>
39 #define FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(classname, type_name) \
40 template <typename TTheClass_> \
41 struct classname##__folly_traits_impl__ { \
42 template <typename UTheClass_> \
43 static std::true_type test(typename UTheClass_::type_name*); \
45 static std::false_type test(...); \
47 template <typename TTheClass_> \
48 using classname = decltype( \
49 classname##__folly_traits_impl__<TTheClass_>::template test<TTheClass_>( \
52 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, cv_qual) \
53 template <typename TTheClass_, typename RTheReturn_, typename... TTheArgs_> \
54 struct classname##__folly_traits_impl__< \
56 RTheReturn_(TTheArgs_...) cv_qual> { \
58 typename UTheClass_, \
59 RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual> \
61 template <typename UTheClass_> \
62 static std::true_type test(sfinae<UTheClass_, &UTheClass_::func_name>*); \
64 static std::false_type test(...); \
68 * The FOLLY_CREATE_HAS_MEMBER_FN_TRAITS is used to create traits
69 * classes that check for the existence of a member function with
70 * a given name and signature. It currently does not support
71 * checking for inherited members.
73 * Such classes receive two template parameters: the class to be checked
74 * and the signature of the member function. A static boolean field
75 * named `value` (which is also constexpr) tells whether such member
78 * Each traits class created is bound only to the member name, not to
79 * its signature nor to the type of the class containing it.
81 * Say you need to know if a given class has a member function named
82 * `test` with the following signature:
86 * You'd need this macro to create a traits class to check for a member
87 * named `test`, and then use this traits class to check for the signature:
91 * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
93 * } // unnamed-namespace
96 * cout << "Does class Foo have a member int test() const? "
97 * << boolalpha << has_test_traits<Foo, int() const>::value;
100 * You can use the same traits class to test for a completely different
101 * signature, on a completely different class, as long as the member name
105 * cout << "Does class Foo have a member int test()? "
106 * << boolalpha << has_test_traits<Foo, int()>::value;
107 * cout << "Does class Foo have a member int test() const? "
108 * << boolalpha << has_test_traits<Foo, int() const>::value;
109 * cout << "Does class Bar have a member double test(const string&, long)? "
110 * << boolalpha << has_test_traits<Bar, double(const string&, long)>::value;
113 * @author: Marcelo Juchem <marcelo@fb.com>
115 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(classname, func_name) \
116 template <typename, typename> \
117 struct classname##__folly_traits_impl__; \
118 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, ); \
119 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, const); \
120 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
121 classname, func_name, /* nolint */ volatile); \
122 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
123 classname, func_name, /* nolint */ volatile const); \
124 template <typename TTheClass_, typename TTheSignature_> \
126 decltype(classname##__folly_traits_impl__<TTheClass_, TTheSignature_>:: \
127 template test<TTheClass_>(nullptr))
136 * using decayed = typename std::decay<T>::type;
138 * With the C++14 standard trait aliases, we could use:
140 * using decayed = std::decay_t<T>;
142 * Without them, we could use:
144 * using decayed = _t<std::decay<T>>;
146 * Also useful for any other library with template types having dependent
147 * member types named `type`, like the standard trait types.
149 template <typename T>
150 using _t = typename T::type;
155 * A type alias for `void`. `void_t` is useful for controling class-template
156 * partial specialization.
160 * // has_value_type<T>::value is true if T has a nested type `value_type`
161 * template <class T, class = void>
162 * struct has_value_type
163 * : std::false_type {};
166 * struct has_value_type<T, folly::void_t<typename T::value_type>>
167 * : std::true_type {};
169 #if defined(__cpp_lib_void_t) || defined(_MSC_VER)
171 /* using override */ using std::void_t;
173 #else // defined(__cpp_lib_void_t) || defined(_MSC_VER)
175 namespace traits_detail {
180 } // namespace traits_detail
182 template <class... Ts>
183 using void_t = _t<traits_detail::void_t_<Ts...>>;
185 #endif // defined(__cpp_lib_void_t) || defined(_MSC_VER)
188 * IsRelocatable<T>::value describes the ability of moving around
189 * memory a value of type T by using memcpy (as opposed to the
190 * conservative approach of calling the copy constructor and then
191 * destroying the old temporary. Essentially for a relocatable type,
192 * the following two sequences of code should be semantically
195 * void move1(T * from, T * to) {
200 * void move2(T * from, T * to) {
201 * memcpy(to, from, sizeof(T));
204 * Most C++ types are relocatable; the ones that aren't would include
205 * internal pointers or (very rarely) would need to update remote
206 * pointers to pointers tracking them. All C++ primitive types and
207 * type constructors are relocatable.
209 * This property can be used in a variety of optimizations. Currently
210 * fbvector uses this property intensively.
212 * The default conservatively assumes the type is not
213 * relocatable. Several specializations are defined for known
214 * types. You may want to add your own specializations. Do so in
215 * namespace folly and make sure you keep the specialization of
216 * IsRelocatable<SomeStruct> in the same header as SomeStruct.
218 * You may also declare a type to be relocatable by including
219 * `typedef std::true_type IsRelocatable;`
220 * in the class header.
222 * It may be unset in a base class by overriding the typedef to false_type.
225 * IsTriviallyCopyable describes the value semantics property. C++11 contains
226 * the type trait is_trivially_copyable; however, it is not yet implemented
227 * in gcc (as of 4.7.1), and the user may wish to specify otherwise.
230 * IsZeroInitializable describes the property that default construction is the
231 * same as memset(dst, 0, sizeof(T)).
234 namespace traits_detail {
236 #define FOLLY_HAS_TRUE_XXX(name) \
237 FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_##name, name); \
239 struct name##_is_true : std::is_same<typename T::name, std::true_type> {}; \
241 struct has_true_##name : std::conditional< \
242 has_##name<T>::value, \
244 std::false_type>::type {};
246 FOLLY_HAS_TRUE_XXX(IsRelocatable)
247 FOLLY_HAS_TRUE_XXX(IsZeroInitializable)
248 FOLLY_HAS_TRUE_XXX(IsTriviallyCopyable)
250 #undef FOLLY_HAS_TRUE_XXX
252 // Older versions of libstdc++ do not provide std::is_trivially_copyable
253 #if defined(__clang__) && !defined(_LIBCPP_VERSION)
255 struct is_trivially_copyable
256 : std::integral_constant<bool, __is_trivially_copyable(T)> {};
257 #elif defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
259 struct is_trivially_copyable : std::is_trivial<T> {};
262 using is_trivially_copyable = std::is_trivially_copyable<T>;
268 /* implicit */ Ignore(const T&) {}
270 const Ignore& operator=(T const&) const { return *this; }
274 using Ignored = Ignore;
276 namespace traits_detail_IsEqualityComparable {
277 Ignore operator==(Ignore, Ignore);
279 template <class T, class U = T>
280 struct IsEqualityComparable
281 : std::is_convertible<
282 decltype(std::declval<T>() == std::declval<U>()),
287 /* using override */ using traits_detail_IsEqualityComparable::
288 IsEqualityComparable;
290 namespace traits_detail_IsLessThanComparable {
291 Ignore operator<(Ignore, Ignore);
293 template <class T, class U = T>
294 struct IsLessThanComparable
295 : std::is_convertible<
296 decltype(std::declval<T>() < std::declval<U>()),
301 /* using override */ using traits_detail_IsLessThanComparable::
302 IsLessThanComparable;
304 namespace traits_detail_IsNothrowSwappable {
305 #if defined(_MSC_VER) || defined(__cpp_lib_is_swappable)
306 // MSVC already implements the C++17 P0185R1 proposal which
307 // adds std::is_nothrow_swappable, so use it instead.
308 template <typename T>
309 using IsNothrowSwappable = std::is_nothrow_swappable<T>;
311 /* using override */ using std::swap;
314 struct IsNothrowSwappable
315 : std::integral_constant<bool,
316 std::is_nothrow_move_constructible<T>::value &&
317 noexcept(swap(std::declval<T&>(), std::declval<T&>()))
322 /* using override */ using traits_detail_IsNothrowSwappable::IsNothrowSwappable;
324 template <class T> struct IsTriviallyCopyable
326 traits_detail::has_IsTriviallyCopyable<T>::value,
327 traits_detail::has_true_IsTriviallyCopyable<T>,
328 traits_detail::is_trivially_copyable<T>
331 template <class T> struct IsRelocatable
333 traits_detail::has_IsRelocatable<T>::value,
334 traits_detail::has_true_IsRelocatable<T>,
335 // TODO add this line (and some tests for it) when we upgrade to gcc 4.7
336 //std::is_trivially_move_constructible<T>::value ||
337 IsTriviallyCopyable<T>
340 template <class T> struct IsZeroInitializable
342 traits_detail::has_IsZeroInitializable<T>::value,
343 traits_detail::has_true_IsZeroInitializable<T>,
344 std::integral_constant<bool, !std::is_class<T>::value>
347 template <typename...>
348 struct Conjunction : std::true_type {};
349 template <typename T>
350 struct Conjunction<T> : T {};
351 template <typename T, typename... TList>
352 struct Conjunction<T, TList...>
353 : std::conditional<T::value, Conjunction<TList...>, T>::type {};
355 template <typename...>
356 struct Disjunction : std::false_type {};
357 template <typename T>
358 struct Disjunction<T> : T {};
359 template <typename T, typename... TList>
360 struct Disjunction<T, TList...>
361 : std::conditional<T::value, T, Disjunction<TList...>>::type {};
363 template <typename T>
364 struct Negation : std::integral_constant<bool, !T::value> {};
366 template <bool... Bs>
368 using valid_type = bool;
369 static constexpr std::size_t size() {
370 return sizeof...(Bs);
374 // Lighter-weight than Conjunction, but evaluates all sub-conditions eagerly.
375 template <class... Ts>
376 using StrictConjunction =
377 std::is_same<Bools<Ts::value..., true>, Bools<true, Ts::value...>>;
382 * Use this macro ONLY inside namespace folly. When using it with a
383 * regular type, use it like this:
385 * // Make sure you're at namespace ::folly scope
386 * template<> FOLLY_ASSUME_RELOCATABLE(MyType)
388 * When using it with a template type, use it like this:
390 * // Make sure you're at namespace ::folly scope
391 * template<class T1, class T2>
392 * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
394 #define FOLLY_ASSUME_RELOCATABLE(...) \
395 struct IsRelocatable< __VA_ARGS__ > : std::true_type {};
398 * The FOLLY_ASSUME_FBVECTOR_COMPATIBLE* macros below encode the
399 * assumption that the type is relocatable per IsRelocatable
400 * above. Many types can be assumed to satisfy this condition, but
401 * it is the responsibility of the user to state that assumption.
402 * User-defined classes will not be optimized for use with
403 * fbvector (see FBVector.h) unless they state that assumption.
405 * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
407 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
409 * The versions FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1, _2, _3, and _4
410 * allow using the macro for describing templatized classes with 1, 2,
411 * 3, and 4 template parameters respectively. For template classes
412 * just use the macro with the appropriate number and pass the name of
413 * the template to it. Example:
415 * template <class T1, class T2> class MyType { ... };
417 * // Make sure you're at global scope
418 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
421 // Use this macro ONLY at global level (no namespace)
422 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE(...) \
425 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__) \
427 // Use this macro ONLY at global level (no namespace)
428 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(...) \
430 template <class T1> \
431 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>) \
433 // Use this macro ONLY at global level (no namespace)
434 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(...) \
436 template <class T1, class T2> \
437 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) \
439 // Use this macro ONLY at global level (no namespace)
440 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(...) \
442 template <class T1, class T2, class T3> \
443 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) \
445 // Use this macro ONLY at global level (no namespace)
446 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(...) \
448 template <class T1, class T2, class T3, class T4> \
449 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) \
453 * Instantiate FOLLY_ASSUME_FBVECTOR_COMPATIBLE for a few types. It is
454 * safe to assume that pair is compatible if both of its components
455 * are. Furthermore, all STL containers can be assumed to comply,
456 * although that is not guaranteed by the standard.
459 FOLLY_NAMESPACE_STD_BEGIN
461 template <class T, class U>
463 #ifndef _GLIBCXX_USE_FB
464 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
465 template <class T, class R, class A>
467 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
469 template <class T, class R, class A, class S>
472 template <class T, class A>
474 template <class T, class A>
476 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
477 template <class T, class A>
479 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
480 template <class T, class C, class A>
482 template <class K, class V, class C, class A>
487 FOLLY_NAMESPACE_STD_END
491 // STL commonly-used types
492 template <class T, class U>
493 struct IsRelocatable< std::pair<T, U> >
494 : std::integral_constant<bool,
495 IsRelocatable<T>::value &&
496 IsRelocatable<U>::value> {};
498 // Is T one of T1, T2, ..., Tn?
499 template <class T, class... Ts>
501 enum { value = false };
504 template <class T, class T1, class... Ts>
505 struct IsOneOf<T, T1, Ts...> {
506 enum { value = std::is_same<T, T1>::value || IsOneOf<T, Ts...>::value };
510 * Complementary type traits for integral comparisons.
512 * For instance, `if(x < 0)` yields an error in clang for unsigned types
513 * when -Werror is used due to -Wtautological-compare
516 * @author: Marcelo Juchem <marcelo@fb.com>
521 template <typename T, bool>
522 struct is_negative_impl {
523 constexpr static bool check(T x) { return x < 0; }
526 template <typename T>
527 struct is_negative_impl<T, false> {
528 constexpr static bool check(T) { return false; }
531 // folly::to integral specializations can end up generating code
532 // inside what are really static ifs (not executed because of the templated
533 // types) that violate -Wsign-compare and/or -Wbool-compare so suppress them
534 // in order to not prevent all calling code from using it.
536 FOLLY_GCC_DISABLE_WARNING(sign-compare)
537 #if __GNUC_PREREQ(5, 0)
538 FOLLY_GCC_DISABLE_WARNING(bool-compare)
540 FOLLY_MSVC_DISABLE_WARNING(4388) // sign-compare
541 FOLLY_MSVC_DISABLE_WARNING(4804) // bool-compare
543 template <typename RHS, RHS rhs, typename LHS>
544 bool less_than_impl(LHS const lhs) {
546 rhs > std::numeric_limits<LHS>::max() ? true :
547 rhs <= std::numeric_limits<LHS>::min() ? false :
551 template <typename RHS, RHS rhs, typename LHS>
552 bool greater_than_impl(LHS const lhs) {
554 rhs > std::numeric_limits<LHS>::max() ? false :
555 rhs < std::numeric_limits<LHS>::min() ? true :
561 } // namespace detail {
564 template <typename T>
565 constexpr bool is_negative(T x) {
566 return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x);
570 template <typename T>
571 constexpr bool is_non_positive(T x) { return !x || folly::is_negative(x); }
574 template <typename T>
575 constexpr bool is_positive(T x) { return !is_non_positive(x); }
578 template <typename T>
579 constexpr bool is_non_negative(T x) {
580 return !x || is_positive(x);
583 template <typename RHS, RHS rhs, typename LHS>
584 bool less_than(LHS const lhs) {
585 return detail::less_than_impl<
586 RHS, rhs, typename std::remove_reference<LHS>::type
590 template <typename RHS, RHS rhs, typename LHS>
591 bool greater_than(LHS const lhs) {
592 return detail::greater_than_impl<
593 RHS, rhs, typename std::remove_reference<LHS>::type
597 namespace traits_detail {
598 struct InPlaceTag {};
600 struct InPlaceTypeTag {};
601 template <std::size_t>
602 struct InPlaceIndexTag {};
606 * Like std::piecewise_construct, a tag type & instance used for in-place
607 * construction of non-movable contained types, e.g. by Synchronized.
608 * Follows the naming and design of std::in_place suggested in
609 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0032r2.pdf
611 using in_place_t = traits_detail::InPlaceTag (&)(traits_detail::InPlaceTag);
614 using in_place_type_t =
615 traits_detail::InPlaceTypeTag<T> (&)(traits_detail::InPlaceTypeTag<T>);
617 template <std::size_t I>
618 using in_place_index_t =
619 traits_detail::InPlaceIndexTag<I> (&)(traits_detail::InPlaceIndexTag<I>);
621 inline traits_detail::InPlaceTag in_place(traits_detail::InPlaceTag = {}) {
626 inline traits_detail::InPlaceTypeTag<T> in_place(
627 traits_detail::InPlaceTypeTag<T> = {}) {
631 template <std::size_t I>
632 inline traits_detail::InPlaceIndexTag<I> in_place(
633 traits_detail::InPlaceIndexTag<I> = {}) {
637 // For backwards compatibility:
638 using construct_in_place_t = in_place_t;
640 inline traits_detail::InPlaceTag construct_in_place(
641 traits_detail::InPlaceTag = {}) {
646 * Initializer lists are a powerful compile time syntax introduced in C++11
647 * but due to their often conflicting syntax they are not used by APIs for
650 * Further standard conforming compilers *strongly* favor an
651 * std::initalizer_list overload for construction if one exists. The
652 * following is a simple tag used to disambiguate construction with
653 * initializer lists and regular uniform initialization.
655 * For example consider the following case
659 * explicit Something(int);
660 * Something(std::intiializer_list<int>);
666 * Something something{1}; // SURPRISE!!
668 * The last call to instantiate the Something object will go to the
669 * initializer_list overload. Which may be surprising to users.
671 * If however this tag was used to disambiguate such construction it would be
672 * easy for users to see which construction overload their code was referring
677 * explicit Something(int);
678 * Something(folly::initlist_construct_t, std::initializer_list<int>);
684 * Something something_one{1}; // not the initializer_list overload
685 * Something something_two{folly::initlist_construct, {1}}; // correct
687 struct initlist_construct_t {};
688 constexpr initlist_construct_t initlist_construct{};
692 // Assume nothing when compiling with MSVC.
694 // gcc-5.0 changed string's implementation in libstdc++ to be non-relocatable
695 #if !_GLIBCXX_USE_CXX11_ABI
696 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string)
698 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector)
699 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::list)
700 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque)
701 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr)
702 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr)
703 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function)
706 /* Some combinations of compilers and C++ libraries make __int128 and
707 * unsigned __int128 available but do not correctly define their standard type
710 * If FOLLY_SUPPLY_MISSING_INT128_TRAITS is defined, we define these traits
713 * @author: Phil Willoughby <philwill@fb.com>
715 #if FOLLY_SUPPLY_MISSING_INT128_TRAITS
716 FOLLY_NAMESPACE_STD_BEGIN
718 struct is_arithmetic<__int128> : ::std::true_type {};
720 struct is_arithmetic<unsigned __int128> : ::std::true_type {};
722 struct is_integral<__int128> : ::std::true_type {};
724 struct is_integral<unsigned __int128> : ::std::true_type {};
726 struct make_unsigned<__int128> {
727 typedef unsigned __int128 type;
730 struct make_signed<__int128> {
731 typedef __int128 type;
734 struct make_unsigned<unsigned __int128> {
735 typedef unsigned __int128 type;
738 struct make_signed<unsigned __int128> {
739 typedef __int128 type;
742 struct is_signed<__int128> : ::std::true_type {};
744 struct is_unsigned<unsigned __int128> : ::std::true_type {};
745 FOLLY_NAMESPACE_STD_END
746 #endif // FOLLY_SUPPLY_MISSING_INT128_TRAITS