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
24 #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 constexpr bool test(typename UTheClass_::type_name*) { \
47 static constexpr bool test(...) { \
51 template <typename TTheClass_> \
52 using classname = typename std::conditional< \
53 classname##__folly_traits_impl__<TTheClass_>::template test<TTheClass_>( \
56 std::false_type>::type;
58 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, cv_qual) \
59 template <typename TTheClass_, typename RTheReturn_, typename... TTheArgs_> \
60 struct classname##__folly_traits_impl__< \
62 RTheReturn_(TTheArgs_...) cv_qual> { \
64 typename UTheClass_, \
65 RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual> \
67 template <typename UTheClass_> \
68 static std::true_type test(sfinae<UTheClass_, &UTheClass_::func_name>*); \
70 static std::false_type test(...); \
74 * The FOLLY_CREATE_HAS_MEMBER_FN_TRAITS is used to create traits
75 * classes that check for the existence of a member function with
76 * a given name and signature. It currently does not support
77 * checking for inherited members.
79 * Such classes receive two template parameters: the class to be checked
80 * and the signature of the member function. A static boolean field
81 * named `value` (which is also constexpr) tells whether such member
84 * Each traits class created is bound only to the member name, not to
85 * its signature nor to the type of the class containing it.
87 * Say you need to know if a given class has a member function named
88 * `test` with the following signature:
92 * You'd need this macro to create a traits class to check for a member
93 * named `test`, and then use this traits class to check for the signature:
97 * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
99 * } // unnamed-namespace
102 * cout << "Does class Foo have a member int test() const? "
103 * << boolalpha << has_test_traits<Foo, int() const>::value;
106 * You can use the same traits class to test for a completely different
107 * signature, on a completely different class, as long as the member name
111 * cout << "Does class Foo have a member int test()? "
112 * << boolalpha << has_test_traits<Foo, int()>::value;
113 * cout << "Does class Foo have a member int test() const? "
114 * << boolalpha << has_test_traits<Foo, int() const>::value;
115 * cout << "Does class Bar have a member double test(const string&, long)? "
116 * << boolalpha << has_test_traits<Bar, double(const string&, long)>::value;
119 * @author: Marcelo Juchem <marcelo@fb.com>
121 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(classname, func_name) \
122 template <typename, typename> \
123 struct classname##__folly_traits_impl__; \
124 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, ); \
125 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, const); \
126 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
127 classname, func_name, /* nolint */ volatile); \
128 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
129 classname, func_name, /* nolint */ volatile const); \
130 template <typename TTheClass_, typename TTheSignature_> \
132 decltype(classname##__folly_traits_impl__<TTheClass_, TTheSignature_>:: \
133 template test<TTheClass_>(nullptr))
142 * using decayed = typename std::decay<T>::type;
144 * With the C++14 standard trait aliases, we could use:
146 * using decayed = std::decay_t<T>;
148 * Without them, we could use:
150 * using decayed = _t<std::decay<T>>;
152 * Also useful for any other library with template types having dependent
153 * member types named `type`, like the standard trait types.
155 template <typename T>
156 using _t = typename T::type;
161 * A type alias for the first template type argument. `type_t` is useful for
162 * controlling class-template and function-template partial specialization.
166 * template <typename Value>
169 * template <typename... Args>
171 * type_t<in_place_t, decltype(Value(std::declval<Args>()...))>,
177 * A type alias for `void`. `void_t` is useful for controling class-template
178 * and function-template partial specialization.
182 * // has_value_type<T>::value is true if T has a nested type `value_type`
183 * template <class T, class = void>
184 * struct has_value_type
185 * : std::false_type {};
188 * struct has_value_type<T, folly::void_t<typename T::value_type>>
189 * : std::true_type {};
193 * There is a bug in libstdc++, libc++, and MSVC's STL that causes it to
194 * ignore unused template parameter arguments in template aliases and does not
195 * cause substitution failures. This defect has been recorded here:
196 * http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558.
198 * This causes the implementation of std::void_t to be buggy, as it is likely
199 * defined as something like the following:
201 * template <typename...>
202 * using void_t = void;
204 * This causes the compiler to ignore all the template arguments and does not
205 * help when one wants to cause substitution failures. Rather declarations
206 * which have void_t in orthogonal specializations are treated as the same.
207 * For example, assuming the possible `T` types are only allowed to have
208 * either the alias `one` or `two` and never both or none:
210 * template <typename T,
211 * typename std::void_t<std::decay_t<T>::one>* = nullptr>
213 * template <typename T,
214 * typename std::void_t<std::decay_t<T>::two>* = nullptr>
217 * The second foo() will be a redefinition because it conflicts with the first
218 * one; void_t does not cause substitution failures - the template types are
222 namespace traits_detail {
228 } // namespace traits_detail
230 template <class T, class... Ts>
231 using type_t = typename traits_detail::type_t_<Ts...>::template apply<T>;
232 template <class... Ts>
233 using void_t = type_t<void, Ts...>;
236 * IsRelocatable<T>::value describes the ability of moving around
237 * memory a value of type T by using memcpy (as opposed to the
238 * conservative approach of calling the copy constructor and then
239 * destroying the old temporary. Essentially for a relocatable type,
240 * the following two sequences of code should be semantically
243 * void move1(T * from, T * to) {
248 * void move2(T * from, T * to) {
249 * memcpy(to, from, sizeof(T));
252 * Most C++ types are relocatable; the ones that aren't would include
253 * internal pointers or (very rarely) would need to update remote
254 * pointers to pointers tracking them. All C++ primitive types and
255 * type constructors are relocatable.
257 * This property can be used in a variety of optimizations. Currently
258 * fbvector uses this property intensively.
260 * The default conservatively assumes the type is not
261 * relocatable. Several specializations are defined for known
262 * types. You may want to add your own specializations. Do so in
263 * namespace folly and make sure you keep the specialization of
264 * IsRelocatable<SomeStruct> in the same header as SomeStruct.
266 * You may also declare a type to be relocatable by including
267 * `typedef std::true_type IsRelocatable;`
268 * in the class header.
270 * It may be unset in a base class by overriding the typedef to false_type.
273 * IsTriviallyCopyable describes the value semantics property. C++11 contains
274 * the type trait is_trivially_copyable; however, it is not yet implemented
275 * in gcc (as of 4.7.1), and the user may wish to specify otherwise.
278 * IsZeroInitializable describes the property that default construction is the
279 * same as memset(dst, 0, sizeof(T)).
282 namespace traits_detail {
284 #define FOLLY_HAS_TRUE_XXX(name) \
285 FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_##name, name); \
287 struct name##_is_true : std::is_same<typename T::name, std::true_type> {}; \
289 struct has_true_##name : std::conditional< \
290 has_##name<T>::value, \
292 std::false_type>::type {};
294 FOLLY_HAS_TRUE_XXX(IsRelocatable)
295 FOLLY_HAS_TRUE_XXX(IsZeroInitializable)
296 FOLLY_HAS_TRUE_XXX(IsTriviallyCopyable)
298 #undef FOLLY_HAS_TRUE_XXX
300 // Older versions of libstdc++ do not provide std::is_trivially_copyable
301 #if defined(__clang__) && !defined(_LIBCPP_VERSION)
303 struct is_trivially_copyable
304 : std::integral_constant<bool, __is_trivially_copyable(T)> {};
305 #elif defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
307 struct is_trivially_copyable : std::is_trivial<T> {};
310 using is_trivially_copyable = std::is_trivially_copyable<T>;
312 } // namespace traits_detail
316 /* implicit */ Ignore(const T&) {}
318 const Ignore& operator=(T const&) const { return *this; }
322 using Ignored = Ignore;
324 namespace traits_detail_IsEqualityComparable {
325 Ignore operator==(Ignore, Ignore);
327 template <class T, class U = T>
328 struct IsEqualityComparable
329 : std::is_convertible<
330 decltype(std::declval<T>() == std::declval<U>()),
333 } // namespace traits_detail_IsEqualityComparable
335 /* using override */ using traits_detail_IsEqualityComparable::
336 IsEqualityComparable;
338 namespace traits_detail_IsLessThanComparable {
339 Ignore operator<(Ignore, Ignore);
341 template <class T, class U = T>
342 struct IsLessThanComparable
343 : std::is_convertible<
344 decltype(std::declval<T>() < std::declval<U>()),
347 } // namespace traits_detail_IsLessThanComparable
349 /* using override */ using traits_detail_IsLessThanComparable::
350 IsLessThanComparable;
352 namespace traits_detail_IsNothrowSwappable {
353 #if defined(__cpp_lib_is_swappable) || (_CPPLIB_VER && _HAS_CXX17)
354 // MSVC 2015+ already implements the C++17 P0185R1 proposal which
355 // adds std::is_nothrow_swappable, so use it instead if C++17 mode
357 template <typename T>
358 using IsNothrowSwappable = std::is_nothrow_swappable<T>;
360 // MSVC 2015+ defines the base even if C++17 is disabled, and
361 // MSVC 2015 has issues with our fallback implementation due to
362 // over-eager evaluation of noexcept.
363 template <typename T>
364 using IsNothrowSwappable = std::_Is_nothrow_swappable<T>;
366 /* using override */ using std::swap;
369 struct IsNothrowSwappable
370 : std::integral_constant<bool,
371 std::is_nothrow_move_constructible<T>::value &&
372 noexcept(swap(std::declval<T&>(), std::declval<T&>()))
375 } // namespace traits_detail_IsNothrowSwappable
377 /* using override */ using traits_detail_IsNothrowSwappable::IsNothrowSwappable;
379 template <class T> struct IsTriviallyCopyable
381 traits_detail::has_IsTriviallyCopyable<T>::value,
382 traits_detail::has_true_IsTriviallyCopyable<T>,
383 traits_detail::is_trivially_copyable<T>
386 template <class T> struct IsRelocatable
388 traits_detail::has_IsRelocatable<T>::value,
389 traits_detail::has_true_IsRelocatable<T>,
390 // TODO add this line (and some tests for it) when we upgrade to gcc 4.7
391 //std::is_trivially_move_constructible<T>::value ||
392 IsTriviallyCopyable<T>
395 template <class T> struct IsZeroInitializable
397 traits_detail::has_IsZeroInitializable<T>::value,
398 traits_detail::has_true_IsZeroInitializable<T>,
399 std::integral_constant<bool, !std::is_class<T>::value>
402 template <typename...>
403 struct Conjunction : std::true_type {};
404 template <typename T>
405 struct Conjunction<T> : T {};
406 template <typename T, typename... TList>
407 struct Conjunction<T, TList...>
408 : std::conditional<T::value, Conjunction<TList...>, T>::type {};
410 template <typename...>
411 struct Disjunction : std::false_type {};
412 template <typename T>
413 struct Disjunction<T> : T {};
414 template <typename T, typename... TList>
415 struct Disjunction<T, TList...>
416 : std::conditional<T::value, T, Disjunction<TList...>>::type {};
418 template <typename T>
419 struct Negation : std::integral_constant<bool, !T::value> {};
421 template <bool... Bs>
423 using valid_type = bool;
424 static constexpr std::size_t size() {
425 return sizeof...(Bs);
429 // Lighter-weight than Conjunction, but evaluates all sub-conditions eagerly.
430 template <class... Ts>
431 struct StrictConjunction
432 : std::is_same<Bools<Ts::value...>, Bools<(Ts::value || true)...>> {};
434 template <class... Ts>
435 struct StrictDisjunction
437 std::is_same<Bools<Ts::value...>, Bools<(Ts::value && false)...>>
443 * Use this macro ONLY inside namespace folly. When using it with a
444 * regular type, use it like this:
446 * // Make sure you're at namespace ::folly scope
447 * template <> FOLLY_ASSUME_RELOCATABLE(MyType)
449 * When using it with a template type, use it like this:
451 * // Make sure you're at namespace ::folly scope
452 * template <class T1, class T2>
453 * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
455 #define FOLLY_ASSUME_RELOCATABLE(...) \
456 struct IsRelocatable< __VA_ARGS__ > : std::true_type {};
459 * The FOLLY_ASSUME_FBVECTOR_COMPATIBLE* macros below encode the
460 * assumption that the type is relocatable per IsRelocatable
461 * above. Many types can be assumed to satisfy this condition, but
462 * it is the responsibility of the user to state that assumption.
463 * User-defined classes will not be optimized for use with
464 * fbvector (see FBVector.h) unless they state that assumption.
466 * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
468 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
470 * The versions FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1, _2, _3, and _4
471 * allow using the macro for describing templatized classes with 1, 2,
472 * 3, and 4 template parameters respectively. For template classes
473 * just use the macro with the appropriate number and pass the name of
474 * the template to it. Example:
476 * template <class T1, class T2> class MyType { ... };
478 * // Make sure you're at global scope
479 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
482 // Use this macro ONLY at global level (no namespace)
483 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE(...) \
486 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__) \
488 // Use this macro ONLY at global level (no namespace)
489 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(...) \
491 template <class T1> \
492 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>) \
494 // Use this macro ONLY at global level (no namespace)
495 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(...) \
497 template <class T1, class T2> \
498 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) \
500 // Use this macro ONLY at global level (no namespace)
501 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(...) \
503 template <class T1, class T2, class T3> \
504 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) \
506 // Use this macro ONLY at global level (no namespace)
507 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(...) \
509 template <class T1, class T2, class T3, class T4> \
510 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) \
514 * Instantiate FOLLY_ASSUME_FBVECTOR_COMPATIBLE for a few types. It is
515 * safe to assume that pair is compatible if both of its components
516 * are. Furthermore, all STL containers can be assumed to comply,
517 * although that is not guaranteed by the standard.
520 FOLLY_NAMESPACE_STD_BEGIN
522 template <class T, class U>
524 #ifndef _GLIBCXX_USE_FB
525 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
526 template <class T, class R, class A>
528 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
530 template <class T, class R, class A, class S>
533 template <class T, class A>
535 template <class T, class A>
537 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
538 template <class T, class A>
540 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
541 template <class T, class C, class A>
543 template <class K, class V, class C, class A>
548 FOLLY_NAMESPACE_STD_END
552 // STL commonly-used types
553 template <class T, class U>
554 struct IsRelocatable< std::pair<T, U> >
555 : std::integral_constant<bool,
556 IsRelocatable<T>::value &&
557 IsRelocatable<U>::value> {};
559 // Is T one of T1, T2, ..., Tn?
560 template <typename T, typename... Ts>
561 using IsOneOf = StrictDisjunction<std::is_same<T, Ts>...>;
564 * Complementary type traits for integral comparisons.
566 * For instance, `if(x < 0)` yields an error in clang for unsigned types
567 * when -Werror is used due to -Wtautological-compare
570 * @author: Marcelo Juchem <marcelo@fb.com>
575 template <typename T, bool>
576 struct is_negative_impl {
577 constexpr static bool check(T x) { return x < 0; }
580 template <typename T>
581 struct is_negative_impl<T, false> {
582 constexpr static bool check(T) { return false; }
585 // folly::to integral specializations can end up generating code
586 // inside what are really static ifs (not executed because of the templated
587 // types) that violate -Wsign-compare and/or -Wbool-compare so suppress them
588 // in order to not prevent all calling code from using it.
590 FOLLY_GCC_DISABLE_WARNING("-Wsign-compare")
591 #if __GNUC_PREREQ(5, 0)
592 FOLLY_GCC_DISABLE_WARNING("-Wbool-compare")
594 FOLLY_MSVC_DISABLE_WARNING(4388) // sign-compare
595 FOLLY_MSVC_DISABLE_WARNING(4804) // bool-compare
597 template <typename RHS, RHS rhs, typename LHS>
598 bool less_than_impl(LHS const lhs) {
600 rhs > std::numeric_limits<LHS>::max() ? true :
601 rhs <= std::numeric_limits<LHS>::min() ? false :
605 template <typename RHS, RHS rhs, typename LHS>
606 bool greater_than_impl(LHS const lhs) {
608 rhs > std::numeric_limits<LHS>::max() ? false :
609 rhs < std::numeric_limits<LHS>::min() ? true :
615 } // namespace detail
618 template <typename T>
619 constexpr bool is_negative(T x) {
620 return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x);
624 template <typename T>
625 constexpr bool is_non_positive(T x) { return !x || folly::is_negative(x); }
628 template <typename T>
629 constexpr bool is_positive(T x) { return !is_non_positive(x); }
632 template <typename T>
633 constexpr bool is_non_negative(T x) {
634 return !x || is_positive(x);
637 template <typename RHS, RHS rhs, typename LHS>
638 bool less_than(LHS const lhs) {
639 return detail::less_than_impl<
640 RHS, rhs, typename std::remove_reference<LHS>::type
644 template <typename RHS, RHS rhs, typename LHS>
645 bool greater_than(LHS const lhs) {
646 return detail::greater_than_impl<
647 RHS, rhs, typename std::remove_reference<LHS>::type
652 // Assume nothing when compiling with MSVC.
654 // gcc-5.0 changed string's implementation in libstdc++ to be non-relocatable
655 #if !_GLIBCXX_USE_CXX11_ABI
656 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string)
658 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector)
659 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::list)
660 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque)
661 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr)
662 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr)
663 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function)
666 /* Some combinations of compilers and C++ libraries make __int128 and
667 * unsigned __int128 available but do not correctly define their standard type
670 * If FOLLY_SUPPLY_MISSING_INT128_TRAITS is defined, we define these traits
673 * @author: Phil Willoughby <philwill@fb.com>
675 #if FOLLY_SUPPLY_MISSING_INT128_TRAITS
676 FOLLY_NAMESPACE_STD_BEGIN
678 struct is_arithmetic<__int128> : ::std::true_type {};
680 struct is_arithmetic<unsigned __int128> : ::std::true_type {};
682 struct is_integral<__int128> : ::std::true_type {};
684 struct is_integral<unsigned __int128> : ::std::true_type {};
686 struct make_unsigned<__int128> {
687 typedef unsigned __int128 type;
690 struct make_signed<__int128> {
691 typedef __int128 type;
694 struct make_unsigned<unsigned __int128> {
695 typedef unsigned __int128 type;
698 struct make_signed<unsigned __int128> {
699 typedef __int128 type;
702 struct is_signed<__int128> : ::std::true_type {};
704 struct is_unsigned<unsigned __int128> : ::std::true_type {};
705 FOLLY_NAMESPACE_STD_END
706 #endif // FOLLY_SUPPLY_MISSING_INT128_TRAITS