2 * Copyright 2016 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 #include <boost/type_traits.hpp>
41 #define FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(classname, type_name) \
42 template <typename TTheClass_> \
43 struct classname##__folly_traits_impl__ { \
44 template <typename UTheClass_> \
45 static std::true_type test(typename UTheClass_::type_name*); \
47 static std::false_type test(...); \
49 template <typename TTheClass_> \
50 using classname = decltype( \
51 classname##__folly_traits_impl__<TTheClass_>::template test<TTheClass_>( \
54 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, cv_qual) \
55 template <typename TTheClass_, typename RTheReturn_, typename... TTheArgs_> \
56 struct classname##__folly_traits_impl__< \
58 RTheReturn_(TTheArgs_...) cv_qual> { \
60 typename UTheClass_, \
61 RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual> \
63 template <typename UTheClass_> \
64 static std::true_type test(sfinae<UTheClass_, &UTheClass_::func_name>*); \
66 static std::false_type test(...); \
70 * The FOLLY_CREATE_HAS_MEMBER_FN_TRAITS is used to create traits
71 * classes that check for the existence of a member function with
72 * a given name and signature. It currently does not support
73 * checking for inherited members.
75 * Such classes receive two template parameters: the class to be checked
76 * and the signature of the member function. A static boolean field
77 * named `value` (which is also constexpr) tells whether such member
80 * Each traits class created is bound only to the member name, not to
81 * its signature nor to the type of the class containing it.
83 * Say you need to know if a given class has a member function named
84 * `test` with the following signature:
88 * You'd need this macro to create a traits class to check for a member
89 * named `test`, and then use this traits class to check for the signature:
93 * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
95 * } // unnamed-namespace
98 * cout << "Does class Foo have a member int test() const? "
99 * << boolalpha << has_test_traits<Foo, int() const>::value;
102 * You can use the same traits class to test for a completely different
103 * signature, on a completely different class, as long as the member name
107 * cout << "Does class Foo have a member int test()? "
108 * << boolalpha << has_test_traits<Foo, int()>::value;
109 * cout << "Does class Foo have a member int test() const? "
110 * << boolalpha << has_test_traits<Foo, int() const>::value;
111 * cout << "Does class Bar have a member double test(const string&, long)? "
112 * << boolalpha << has_test_traits<Bar, double(const string&, long)>::value;
115 * @author: Marcelo Juchem <marcelo@fb.com>
117 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(classname, func_name) \
118 template <typename, typename> \
119 struct classname##__folly_traits_impl__; \
120 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, ); \
121 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, const); \
122 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
123 classname, func_name, /* nolint */ volatile); \
124 FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
125 classname, func_name, /* nolint */ volatile const); \
126 template <typename TTheClass_, typename TTheSignature_> \
128 decltype(classname##__folly_traits_impl__<TTheClass_, TTheSignature_>:: \
129 template test<TTheClass_>(nullptr))
134 * IsRelocatable<T>::value describes the ability of moving around
135 * memory a value of type T by using memcpy (as opposed to the
136 * conservative approach of calling the copy constructor and then
137 * destroying the old temporary. Essentially for a relocatable type,
138 * the following two sequences of code should be semantically
141 * void move1(T * from, T * to) {
146 * void move2(T * from, T * to) {
147 * memcpy(to, from, sizeof(T));
150 * Most C++ types are relocatable; the ones that aren't would include
151 * internal pointers or (very rarely) would need to update remote
152 * pointers to pointers tracking them. All C++ primitive types and
153 * type constructors are relocatable.
155 * This property can be used in a variety of optimizations. Currently
156 * fbvector uses this property intensively.
158 * The default conservatively assumes the type is not
159 * relocatable. Several specializations are defined for known
160 * types. You may want to add your own specializations. Do so in
161 * namespace folly and make sure you keep the specialization of
162 * IsRelocatable<SomeStruct> in the same header as SomeStruct.
164 * You may also declare a type to be relocatable by including
165 * `typedef std::true_type IsRelocatable;`
166 * in the class header.
168 * It may be unset in a base class by overriding the typedef to false_type.
171 * IsTriviallyCopyable describes the value semantics property. C++11 contains
172 * the type trait is_trivially_copyable; however, it is not yet implemented
173 * in gcc (as of 4.7.1), and the user may wish to specify otherwise.
176 * IsZeroInitializable describes the property that default construction is the
177 * same as memset(dst, 0, sizeof(T)).
180 namespace traits_detail {
182 #define FOLLY_HAS_TRUE_XXX(name) \
183 FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_##name, name); \
185 struct name##_is_true : std::is_same<typename T::name, std::true_type> {}; \
187 struct has_true_##name : std::conditional< \
188 has_##name<T>::value, \
190 std::false_type>::type {};
192 FOLLY_HAS_TRUE_XXX(IsRelocatable)
193 FOLLY_HAS_TRUE_XXX(IsZeroInitializable)
194 FOLLY_HAS_TRUE_XXX(IsTriviallyCopyable)
196 #undef FOLLY_HAS_TRUE_XXX
198 // Older versions of libstdc++ do not provide std::is_trivially_copyable
199 #if defined(__clang__) && !defined(_LIBCPP_VERSION)
201 struct is_trivially_copyable
202 : std::integral_constant<bool, __is_trivially_copyable(T)> {};
203 #elif defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
205 struct is_trivially_copyable : std::is_trivial<T> {};
208 using is_trivially_copyable = std::is_trivially_copyable<T>;
214 /* implicit */ Ignore(const T&) {}
216 const Ignore& operator=(T const&) const { return *this; }
220 using Ignored = Ignore;
222 namespace traits_detail_IsEqualityComparable {
223 Ignore operator==(Ignore, Ignore);
225 template <class T, class U = T>
226 struct IsEqualityComparable
227 : std::is_convertible<
228 decltype(std::declval<T>() == std::declval<U>()),
233 /* using override */ using traits_detail_IsEqualityComparable::
234 IsEqualityComparable;
236 namespace traits_detail_IsLessThanComparable {
237 Ignore operator<(Ignore, Ignore);
239 template <class T, class U = T>
240 struct IsLessThanComparable
241 : std::is_convertible<
242 decltype(std::declval<T>() < std::declval<U>()),
247 /* using override */ using traits_detail_IsLessThanComparable::
248 IsLessThanComparable;
250 namespace traits_detail_IsNothrowSwappable {
251 #if defined(_MSC_VER) || defined(__cpp_lib_is_swappable)
252 // MSVC already implements the C++17 P0185R1 proposal which
253 // adds std::is_nothrow_swappable, so use it instead.
254 template <typename T>
255 using IsNothrowSwappable = std::is_nothrow_swappable<T>;
257 /* using override */ using std::swap;
260 struct IsNothrowSwappable
261 : std::integral_constant<bool,
262 std::is_nothrow_move_constructible<T>::value &&
263 noexcept(swap(std::declval<T&>(), std::declval<T&>()))
268 /* using override */ using traits_detail_IsNothrowSwappable::IsNothrowSwappable;
270 template <class T> struct IsTriviallyCopyable
272 traits_detail::has_IsTriviallyCopyable<T>::value,
273 traits_detail::has_true_IsTriviallyCopyable<T>,
274 traits_detail::is_trivially_copyable<T>
277 template <class T> struct IsRelocatable
279 traits_detail::has_IsRelocatable<T>::value,
280 traits_detail::has_true_IsRelocatable<T>,
281 // TODO add this line (and some tests for it) when we upgrade to gcc 4.7
282 //std::is_trivially_move_constructible<T>::value ||
283 IsTriviallyCopyable<T>
286 template <class T> struct IsZeroInitializable
288 traits_detail::has_IsZeroInitializable<T>::value,
289 traits_detail::has_true_IsZeroInitializable<T>,
290 std::integral_constant<bool, !std::is_class<T>::value>
293 template <typename...>
294 struct Conjunction : std::true_type {};
295 template <typename T>
296 struct Conjunction<T> : T {};
297 template <typename T, typename... TList>
298 struct Conjunction<T, TList...>
299 : std::conditional<T::value, Conjunction<TList...>, T>::type {};
301 template <typename...>
302 struct Disjunction : std::false_type {};
303 template <typename T>
304 struct Disjunction<T> : T {};
305 template <typename T, typename... TList>
306 struct Disjunction<T, TList...>
307 : std::conditional<T::value, T, Disjunction<TList...>>::type {};
309 template <typename T>
310 struct Negation : std::integral_constant<bool, !T::value> {};
312 template <bool... Bs>
314 using valid_type = bool;
315 static constexpr std::size_t size() {
316 return sizeof...(Bs);
320 // Lighter-weight than Conjunction, but evaluates all sub-conditions eagerly.
321 template <class... Ts>
322 using StrictConjunction =
323 std::is_same<Bools<Ts::value..., true>, Bools<true, Ts::value...>>;
328 * Use this macro ONLY inside namespace folly. When using it with a
329 * regular type, use it like this:
331 * // Make sure you're at namespace ::folly scope
332 * template<> FOLLY_ASSUME_RELOCATABLE(MyType)
334 * When using it with a template type, use it like this:
336 * // Make sure you're at namespace ::folly scope
337 * template<class T1, class T2>
338 * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
340 #define FOLLY_ASSUME_RELOCATABLE(...) \
341 struct IsRelocatable< __VA_ARGS__ > : std::true_type {};
344 * Use this macro ONLY inside namespace boost. When using it with a
345 * regular type, use it like this:
347 * // Make sure you're at namespace ::boost scope
348 * template<> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType)
350 * When using it with a template type, use it like this:
352 * // Make sure you're at namespace ::boost scope
353 * template<class T1, class T2>
354 * FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType<T1, T2>)
356 #define FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(...) \
357 struct has_nothrow_constructor< __VA_ARGS__ > : ::boost::true_type {};
360 * The FOLLY_ASSUME_FBVECTOR_COMPATIBLE* macros below encode two
361 * assumptions: first, that the type is relocatable per IsRelocatable
362 * above, and that it has a nothrow constructor. Most types can be
363 * assumed to satisfy both conditions, but it is the responsibility of
364 * the user to state that assumption. User-defined classes will not
365 * work with fbvector (see FBVector.h) unless they state this
366 * combination of properties.
368 * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
370 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
372 * The versions FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1, _2, _3, and _4
373 * allow using the macro for describing templatized classes with 1, 2,
374 * 3, and 4 template parameters respectively. For template classes
375 * just use the macro with the appropriate number and pass the name of
376 * the template to it. Example:
378 * template <class T1, class T2> class MyType { ... };
380 * // Make sure you're at global scope
381 * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
384 // Use this macro ONLY at global level (no namespace)
385 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE(...) \
386 namespace folly { template<> FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__) } \
388 template<> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__) }
389 // Use this macro ONLY at global level (no namespace)
390 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(...) \
392 template <class T1> FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>) } \
394 template <class T1> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1>) }
395 // Use this macro ONLY at global level (no namespace)
396 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(...) \
398 template <class T1, class T2> \
399 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) } \
401 template <class T1, class T2> \
402 FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2>) }
403 // Use this macro ONLY at global level (no namespace)
404 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(...) \
406 template <class T1, class T2, class T3> \
407 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) } \
409 template <class T1, class T2, class T3> \
410 FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2, T3>) }
411 // Use this macro ONLY at global level (no namespace)
412 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(...) \
414 template <class T1, class T2, class T3, class T4> \
415 FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) } \
417 template <class T1, class T2, class T3, class T4> \
418 FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2, T3, T4>) }
421 * Instantiate FOLLY_ASSUME_FBVECTOR_COMPATIBLE for a few types. It is
422 * safe to assume that pair is compatible if both of its components
423 * are. Furthermore, all STL containers can be assumed to comply,
424 * although that is not guaranteed by the standard.
427 FOLLY_NAMESPACE_STD_BEGIN
429 template <class T, class U>
431 #ifndef _GLIBCXX_USE_FB
432 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
433 template <class T, class R, class A>
435 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
437 template <class T, class R, class A, class S>
440 template <class T, class A>
442 template <class T, class A>
444 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
445 template <class T, class A>
447 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
448 template <class T, class C, class A>
450 template <class K, class V, class C, class A>
455 FOLLY_NAMESPACE_STD_END
459 template <class T> class shared_ptr;
461 template <class T, class U>
462 struct has_nothrow_constructor< std::pair<T, U> >
463 : std::integral_constant<bool,
464 has_nothrow_constructor<T>::value &&
465 has_nothrow_constructor<U>::value> {};
471 // STL commonly-used types
472 template <class T, class U>
473 struct IsRelocatable< std::pair<T, U> >
474 : std::integral_constant<bool,
475 IsRelocatable<T>::value &&
476 IsRelocatable<U>::value> {};
478 // Is T one of T1, T2, ..., Tn?
479 template <class T, class... Ts>
481 enum { value = false };
484 template <class T, class T1, class... Ts>
485 struct IsOneOf<T, T1, Ts...> {
486 enum { value = std::is_same<T, T1>::value || IsOneOf<T, Ts...>::value };
490 * Complementary type traits for integral comparisons.
492 * For instance, `if(x < 0)` yields an error in clang for unsigned types
493 * when -Werror is used due to -Wtautological-compare
496 * @author: Marcelo Juchem <marcelo@fb.com>
501 template <typename T, bool>
502 struct is_negative_impl {
503 constexpr static bool check(T x) { return x < 0; }
506 template <typename T>
507 struct is_negative_impl<T, false> {
508 constexpr static bool check(T) { return false; }
511 // folly::to integral specializations can end up generating code
512 // inside what are really static ifs (not executed because of the templated
513 // types) that violate -Wsign-compare and/or -Wbool-compare so suppress them
514 // in order to not prevent all calling code from using it.
516 FOLLY_GCC_DISABLE_WARNING(sign-compare)
517 #if __GNUC_PREREQ(5, 0)
518 FOLLY_GCC_DISABLE_WARNING(bool-compare)
520 FOLLY_MSVC_DISABLE_WARNING(4388) // sign-compare
521 FOLLY_MSVC_DISABLE_WARNING(4804) // bool-compare
523 template <typename RHS, RHS rhs, typename LHS>
524 bool less_than_impl(LHS const lhs) {
526 rhs > std::numeric_limits<LHS>::max() ? true :
527 rhs <= std::numeric_limits<LHS>::min() ? false :
531 template <typename RHS, RHS rhs, typename LHS>
532 bool greater_than_impl(LHS const lhs) {
534 rhs > std::numeric_limits<LHS>::max() ? false :
535 rhs < std::numeric_limits<LHS>::min() ? true :
541 } // namespace detail {
544 template <typename T>
545 constexpr bool is_negative(T x) {
546 return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x);
550 template <typename T>
551 constexpr bool is_non_positive(T x) { return !x || folly::is_negative(x); }
554 template <typename T>
555 constexpr bool is_positive(T x) { return !is_non_positive(x); }
558 template <typename T>
559 constexpr bool is_non_negative(T x) {
560 return !x || is_positive(x);
563 template <typename RHS, RHS rhs, typename LHS>
564 bool less_than(LHS const lhs) {
565 return detail::less_than_impl<
566 RHS, rhs, typename std::remove_reference<LHS>::type
570 template <typename RHS, RHS rhs, typename LHS>
571 bool greater_than(LHS const lhs) {
572 return detail::greater_than_impl<
573 RHS, rhs, typename std::remove_reference<LHS>::type
577 namespace traits_detail {
578 struct InPlaceTag {};
580 struct InPlaceTypeTag {};
581 template <std::size_t>
582 struct InPlaceIndexTag {};
586 * Like std::piecewise_construct, a tag type & instance used for in-place
587 * construction of non-movable contained types, e.g. by Synchronized.
588 * Follows the naming and design of std::in_place suggested in
589 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0032r2.pdf
591 using in_place_t = traits_detail::InPlaceTag (&)(traits_detail::InPlaceTag);
594 using in_place_type_t =
595 traits_detail::InPlaceTypeTag<T> (&)(traits_detail::InPlaceTypeTag<T>);
597 template <std::size_t I>
598 using in_place_index_t =
599 traits_detail::InPlaceIndexTag<I> (&)(traits_detail::InPlaceIndexTag<I>);
601 inline traits_detail::InPlaceTag in_place(traits_detail::InPlaceTag = {}) {
606 inline traits_detail::InPlaceTypeTag<T> in_place(
607 traits_detail::InPlaceTypeTag<T> = {}) {
611 template <std::size_t I>
612 inline traits_detail::InPlaceIndexTag<I> in_place(
613 traits_detail::InPlaceIndexTag<I> = {}) {
617 // For backwards compatibility:
618 using construct_in_place_t = in_place_t;
620 inline traits_detail::InPlaceTag construct_in_place(
621 traits_detail::InPlaceTag = {}) {
626 * Initializer lists are a powerful compile time syntax introduced in C++11
627 * but due to their often conflicting syntax they are not used by APIs for
630 * Further standard conforming compilers *strongly* favor an
631 * std::initalizer_list overload for construction if one exists. The
632 * following is a simple tag used to disambiguate construction with
633 * initializer lists and regular uniform initialization.
635 * For example consider the following case
639 * explicit Something(int);
640 * Something(std::intiializer_list<int>);
646 * Something something{1}; // SURPRISE!!
648 * The last call to instantiate the Something object will go to the
649 * initializer_list overload. Which may be surprising to users.
651 * If however this tag was used to disambiguate such construction it would be
652 * easy for users to see which construction overload their code was referring
657 * explicit Something(int);
658 * Something(folly::initlist_construct_t, std::initializer_list<int>);
664 * Something something_one{1}; // not the initializer_list overload
665 * Something something_two{folly::initlist_construct, {1}}; // correct
667 struct initlist_construct_t {};
668 constexpr initlist_construct_t initlist_construct{};
672 // Assume nothing when compiling with MSVC.
674 // gcc-5.0 changed string's implementation in libgcc to be non-relocatable
676 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string)
678 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector)
679 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::list)
680 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque)
681 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr)
682 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr)
683 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function)
686 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(boost::shared_ptr)
689 /* Some combinations of compilers and C++ libraries make __int128 and
690 * unsigned __int128 available but do not correctly define their standard type
693 * If FOLLY_SUPPLY_MISSING_INT128_TRAITS is defined, we define these traits
696 * @author: Phil Willoughby <philwill@fb.com>
698 #if FOLLY_SUPPLY_MISSING_INT128_TRAITS
699 FOLLY_NAMESPACE_STD_BEGIN
701 struct is_arithmetic<__int128> : ::std::true_type {};
703 struct is_arithmetic<unsigned __int128> : ::std::true_type {};
705 struct is_integral<__int128> : ::std::true_type {};
707 struct is_integral<unsigned __int128> : ::std::true_type {};
709 struct make_unsigned<__int128> {
710 typedef unsigned __int128 type;
713 struct make_signed<__int128> {
714 typedef __int128 type;
717 struct make_unsigned<unsigned __int128> {
718 typedef unsigned __int128 type;
721 struct make_signed<unsigned __int128> {
722 typedef __int128 type;
725 struct is_signed<__int128> : ::std::true_type {};
727 struct is_unsigned<unsigned __int128> : ::std::true_type {};
728 FOLLY_NAMESPACE_STD_END
729 #endif // FOLLY_SUPPLY_MISSING_INT128_TRAITS