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 `void`. `void_t` is useful for controling class-template
162 * partial specialization.
166 * // has_value_type<T>::value is true if T has a nested type `value_type`
167 * template <class T, class = void>
168 * struct has_value_type
169 * : std::false_type {};
172 * struct has_value_type<T, folly::void_t<typename T::value_type>>
173 * : std::true_type {};
177 * There is a bug in gcc that causes it to ignore unused template parameter
178 * arguments in template aliases and does not cause substitution failures.
179 * This defect has been recorded here:
180 * http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558.
182 * This causes the implementation of std::void_t to be buggy, as it is likely
183 * defined as something like the following:
185 * template <typename...>
186 * using void_t = void;
188 * This causes the compiler to ignore all the template arguments and does not
189 * help when one wants to cause substitution failures. Rather declarations
190 * which have void_t in orthogonal specializations are treated as the same.
191 * For example, assuming the possible `T` types are only allowed to have
192 * either the alias `one` or `two` and never both or none:
194 * template <typename T,
195 * typename std::void_t<std::decay_t<T>::one>* = nullptr>
197 * template <typename T,
198 * typename std::void_t<std::decay_t<T>::two>* = nullptr>
201 * The second foo() will be a redefinition because it conflicts with the first
202 * one; void_t does not cause substitution failures - the template types are
205 * Till then only the non-buggy MSVC std::void_t can be used, and for the rest
206 * folly::void_t will continue to be used because it does not use unnamed
207 * template parameters for the top level implementation of void_t.
209 #if defined(_MSC_VER)
211 /* using override */ using std::void_t;
213 #else // defined(_MSC_VER)
215 namespace traits_detail {
220 } // namespace traits_detail
222 template <class... Ts>
223 using void_t = _t<traits_detail::void_t_<Ts...>>;
225 #endif // defined(_MSC_VER)
228 * IsRelocatable<T>::value describes the ability of moving around
229 * memory a value of type T by using memcpy (as opposed to the
230 * conservative approach of calling the copy constructor and then
231 * destroying the old temporary. Essentially for a relocatable type,
232 * the following two sequences of code should be semantically
235 * void move1(T * from, T * to) {
240 * void move2(T * from, T * to) {
241 * memcpy(to, from, sizeof(T));
244 * Most C++ types are relocatable; the ones that aren't would include
245 * internal pointers or (very rarely) would need to update remote
246 * pointers to pointers tracking them. All C++ primitive types and
247 * type constructors are relocatable.
249 * This property can be used in a variety of optimizations. Currently
250 * fbvector uses this property intensively.
252 * The default conservatively assumes the type is not
253 * relocatable. Several specializations are defined for known
254 * types. You may want to add your own specializations. Do so in
255 * namespace folly and make sure you keep the specialization of
256 * IsRelocatable<SomeStruct> in the same header as SomeStruct.
258 * You may also declare a type to be relocatable by including
259 * `typedef std::true_type IsRelocatable;`
260 * in the class header.
262 * It may be unset in a base class by overriding the typedef to false_type.
265 * IsTriviallyCopyable describes the value semantics property. C++11 contains
266 * the type trait is_trivially_copyable; however, it is not yet implemented
267 * in gcc (as of 4.7.1), and the user may wish to specify otherwise.
270 * IsZeroInitializable describes the property that default construction is the
271 * same as memset(dst, 0, sizeof(T)).
274 namespace traits_detail {
276 #define FOLLY_HAS_TRUE_XXX(name) \
277 FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_##name, name); \
279 struct name##_is_true : std::is_same<typename T::name, std::true_type> {}; \
281 struct has_true_##name : std::conditional< \
282 has_##name<T>::value, \
284 std::false_type>::type {};
286 FOLLY_HAS_TRUE_XXX(IsRelocatable)
287 FOLLY_HAS_TRUE_XXX(IsZeroInitializable)
288 FOLLY_HAS_TRUE_XXX(IsTriviallyCopyable)
290 #undef FOLLY_HAS_TRUE_XXX
292 // Older versions of libstdc++ do not provide std::is_trivially_copyable
293 #if defined(__clang__) && !defined(_LIBCPP_VERSION)
295 struct is_trivially_copyable
296 : std::integral_constant<bool, __is_trivially_copyable(T)> {};
297 #elif defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
299 struct is_trivially_copyable : std::is_trivial<T> {};
302 using is_trivially_copyable = std::is_trivially_copyable<T>;
308 /* implicit */ Ignore(const T&) {}
310 const Ignore& operator=(T const&) const { return *this; }
314 using Ignored = Ignore;
316 namespace traits_detail_IsEqualityComparable {
317 Ignore operator==(Ignore, Ignore);
319 template <class T, class U = T>
320 struct IsEqualityComparable
321 : std::is_convertible<
322 decltype(std::declval<T>() == std::declval<U>()),
327 /* using override */ using traits_detail_IsEqualityComparable::
328 IsEqualityComparable;
330 namespace traits_detail_IsLessThanComparable {
331 Ignore operator<(Ignore, Ignore);
333 template <class T, class U = T>
334 struct IsLessThanComparable
335 : std::is_convertible<
336 decltype(std::declval<T>() < std::declval<U>()),
341 /* using override */ using traits_detail_IsLessThanComparable::
342 IsLessThanComparable;
344 namespace traits_detail_IsNothrowSwappable {
345 #if defined(__cpp_lib_is_swappable) || (_CPPLIB_VER && _HAS_CXX17)
346 // MSVC 2015+ already implements the C++17 P0185R1 proposal which
347 // adds std::is_nothrow_swappable, so use it instead if C++17 mode
349 template <typename T>
350 using IsNothrowSwappable = std::is_nothrow_swappable<T>;
352 // MSVC 2015+ defines the base even if C++17 is disabled, and
353 // MSVC 2015 has issues with our fallback implementation due to
354 // over-eager evaluation of noexcept.
355 template <typename T>
356 using IsNothrowSwappable = std::_Is_nothrow_swappable<T>;
358 /* using override */ using std::swap;
361 struct IsNothrowSwappable
362 : std::integral_constant<bool,
363 std::is_nothrow_move_constructible<T>::value &&
364 noexcept(swap(std::declval<T&>(), std::declval<T&>()))
369 /* using override */ using traits_detail_IsNothrowSwappable::IsNothrowSwappable;
371 template <class T> struct IsTriviallyCopyable
373 traits_detail::has_IsTriviallyCopyable<T>::value,
374 traits_detail::has_true_IsTriviallyCopyable<T>,
375 traits_detail::is_trivially_copyable<T>
378 template <class T> struct IsRelocatable
380 traits_detail::has_IsRelocatable<T>::value,
381 traits_detail::has_true_IsRelocatable<T>,
382 // TODO add this line (and some tests for it) when we upgrade to gcc 4.7
383 //std::is_trivially_move_constructible<T>::value ||
384 IsTriviallyCopyable<T>
387 template <class T> struct IsZeroInitializable
389 traits_detail::has_IsZeroInitializable<T>::value,
390 traits_detail::has_true_IsZeroInitializable<T>,
391 std::integral_constant<bool, !std::is_class<T>::value>
394 template <typename...>
395 struct Conjunction : std::true_type {};
396 template <typename T>
397 struct Conjunction<T> : T {};
398 template <typename T, typename... TList>
399 struct Conjunction<T, TList...>
400 : std::conditional<T::value, Conjunction<TList...>, T>::type {};
402 template <typename...>
403 struct Disjunction : std::false_type {};
404 template <typename T>
405 struct Disjunction<T> : T {};
406 template <typename T, typename... TList>
407 struct Disjunction<T, TList...>
408 : std::conditional<T::value, T, Disjunction<TList...>>::type {};
410 template <typename T>
411 struct Negation : std::integral_constant<bool, !T::value> {};
413 template <bool... Bs>
415 using valid_type = bool;
416 static constexpr std::size_t size() {
417 return sizeof...(Bs);
421 // Lighter-weight than Conjunction, but evaluates all sub-conditions eagerly.
422 template <class... Ts>
423 struct StrictConjunction
424 : std::is_same<Bools<Ts::value...>, Bools<(Ts::value || true)...>> {};
426 template <class... Ts>
427 struct StrictDisjunction
429 std::is_same<Bools<Ts::value...>, Bools<(Ts::value && false)...>>
435 * Use this macro ONLY inside namespace folly. When using it with a
436 * regular type, use it like this:
438 * // Make sure you're at namespace ::folly scope
439 * template <> FOLLY_ASSUME_RELOCATABLE(MyType)
441 * When using it with a template type, use it like this:
443 * // Make sure you're at namespace ::folly scope
444 * template <class T1, class T2>
445 * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
447 #define FOLLY_ASSUME_RELOCATABLE(...) \
448 struct IsRelocatable< __VA_ARGS__ > : std::true_type {};
451 * The FOLLY_ASSUME_FBVECTOR_COMPATIBLE* macros below encode the
452 * assumption that the type is relocatable per IsRelocatable
453 * above. Many types can be assumed to satisfy this condition, but
454 * it is the responsibility of the user to state that assumption.
455 * User-defined classes will not be optimized for use with
456 * fbvector (see FBVector.h) unless they state that assumption.
458 * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
460 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
462 * The versions FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1, _2, _3, and _4
463 * allow using the macro for describing templatized classes with 1, 2,
464 * 3, and 4 template parameters respectively. For template classes
465 * just use the macro with the appropriate number and pass the name of
466 * the template to it. Example:
468 * template <class T1, class T2> class MyType { ... };
470 * // Make sure you're at global scope
471 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
474 // Use this macro ONLY at global level (no namespace)
475 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE(...) \
478 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__) \
480 // Use this macro ONLY at global level (no namespace)
481 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(...) \
483 template <class T1> \
484 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>) \
486 // Use this macro ONLY at global level (no namespace)
487 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(...) \
489 template <class T1, class T2> \
490 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) \
492 // Use this macro ONLY at global level (no namespace)
493 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(...) \
495 template <class T1, class T2, class T3> \
496 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) \
498 // Use this macro ONLY at global level (no namespace)
499 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(...) \
501 template <class T1, class T2, class T3, class T4> \
502 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) \
506 * Instantiate FOLLY_ASSUME_FBVECTOR_COMPATIBLE for a few types. It is
507 * safe to assume that pair is compatible if both of its components
508 * are. Furthermore, all STL containers can be assumed to comply,
509 * although that is not guaranteed by the standard.
512 FOLLY_NAMESPACE_STD_BEGIN
514 template <class T, class U>
516 #ifndef _GLIBCXX_USE_FB
517 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
518 template <class T, class R, class A>
520 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
522 template <class T, class R, class A, class S>
525 template <class T, class A>
527 template <class T, class A>
529 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
530 template <class T, class A>
532 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
533 template <class T, class C, class A>
535 template <class K, class V, class C, class A>
540 FOLLY_NAMESPACE_STD_END
544 // STL commonly-used types
545 template <class T, class U>
546 struct IsRelocatable< std::pair<T, U> >
547 : std::integral_constant<bool,
548 IsRelocatable<T>::value &&
549 IsRelocatable<U>::value> {};
551 // Is T one of T1, T2, ..., Tn?
552 template <typename T, typename... Ts>
553 using IsOneOf = StrictDisjunction<std::is_same<T, Ts>...>;
556 * Complementary type traits for integral comparisons.
558 * For instance, `if(x < 0)` yields an error in clang for unsigned types
559 * when -Werror is used due to -Wtautological-compare
562 * @author: Marcelo Juchem <marcelo@fb.com>
567 template <typename T, bool>
568 struct is_negative_impl {
569 constexpr static bool check(T x) { return x < 0; }
572 template <typename T>
573 struct is_negative_impl<T, false> {
574 constexpr static bool check(T) { return false; }
577 // folly::to integral specializations can end up generating code
578 // inside what are really static ifs (not executed because of the templated
579 // types) that violate -Wsign-compare and/or -Wbool-compare so suppress them
580 // in order to not prevent all calling code from using it.
582 FOLLY_GCC_DISABLE_WARNING("-Wsign-compare")
583 #if __GNUC_PREREQ(5, 0)
584 FOLLY_GCC_DISABLE_WARNING("-Wbool-compare")
586 FOLLY_MSVC_DISABLE_WARNING(4388) // sign-compare
587 FOLLY_MSVC_DISABLE_WARNING(4804) // bool-compare
589 template <typename RHS, RHS rhs, typename LHS>
590 bool less_than_impl(LHS const lhs) {
592 rhs > std::numeric_limits<LHS>::max() ? true :
593 rhs <= std::numeric_limits<LHS>::min() ? false :
597 template <typename RHS, RHS rhs, typename LHS>
598 bool greater_than_impl(LHS const lhs) {
600 rhs > std::numeric_limits<LHS>::max() ? false :
601 rhs < std::numeric_limits<LHS>::min() ? true :
607 } // namespace detail
610 template <typename T>
611 constexpr bool is_negative(T x) {
612 return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x);
616 template <typename T>
617 constexpr bool is_non_positive(T x) { return !x || folly::is_negative(x); }
620 template <typename T>
621 constexpr bool is_positive(T x) { return !is_non_positive(x); }
624 template <typename T>
625 constexpr bool is_non_negative(T x) {
626 return !x || is_positive(x);
629 template <typename RHS, RHS rhs, typename LHS>
630 bool less_than(LHS const lhs) {
631 return detail::less_than_impl<
632 RHS, rhs, typename std::remove_reference<LHS>::type
636 template <typename RHS, RHS rhs, typename LHS>
637 bool greater_than(LHS const lhs) {
638 return detail::greater_than_impl<
639 RHS, rhs, typename std::remove_reference<LHS>::type
644 // Assume nothing when compiling with MSVC.
646 // gcc-5.0 changed string's implementation in libstdc++ to be non-relocatable
647 #if !_GLIBCXX_USE_CXX11_ABI
648 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string)
650 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector)
651 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::list)
652 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque)
653 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr)
654 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr)
655 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function)
658 /* Some combinations of compilers and C++ libraries make __int128 and
659 * unsigned __int128 available but do not correctly define their standard type
662 * If FOLLY_SUPPLY_MISSING_INT128_TRAITS is defined, we define these traits
665 * @author: Phil Willoughby <philwill@fb.com>
667 #if FOLLY_SUPPLY_MISSING_INT128_TRAITS
668 FOLLY_NAMESPACE_STD_BEGIN
670 struct is_arithmetic<__int128> : ::std::true_type {};
672 struct is_arithmetic<unsigned __int128> : ::std::true_type {};
674 struct is_integral<__int128> : ::std::true_type {};
676 struct is_integral<unsigned __int128> : ::std::true_type {};
678 struct make_unsigned<__int128> {
679 typedef unsigned __int128 type;
682 struct make_signed<__int128> {
683 typedef __int128 type;
686 struct make_unsigned<unsigned __int128> {
687 typedef unsigned __int128 type;
690 struct make_signed<unsigned __int128> {
691 typedef __int128 type;
694 struct is_signed<__int128> : ::std::true_type {};
696 struct is_unsigned<unsigned __int128> : ::std::true_type {};
697 FOLLY_NAMESPACE_STD_END
698 #endif // FOLLY_SUPPLY_MISSING_INT128_TRAITS