Thread-safe version of loopKeepAlive()
[folly.git] / folly / Traits.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // @author: Andrei Alexandrescu
18
19 #pragma once
20
21 #include <memory>
22 #include <limits>
23 #include <type_traits>
24 #include <functional>
25
26 #include <folly/Portability.h>
27
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
33 // c++config.h:
34 //   _LIBSTDCXX_FBSTRING - Set inside libstdc++.  This is useful to
35 //      gate use inside fbcode v. libstdc++
36 #include <bits/c++config.h>
37 #endif
38
39 #include <boost/type_traits.hpp>
40 #include <boost/mpl/has_xxx.hpp>
41
42 namespace folly {
43
44 /**
45  * IsRelocatable<T>::value describes the ability of moving around
46  * memory a value of type T by using memcpy (as opposed to the
47  * conservative approach of calling the copy constructor and then
48  * destroying the old temporary. Essentially for a relocatable type,
49  * the following two sequences of code should be semantically
50  * equivalent:
51  *
52  * void move1(T * from, T * to) {
53  *   new(to) T(from);
54  *   (*from).~T();
55  * }
56  *
57  * void move2(T * from, T * to) {
58  *   memcpy(to, from, sizeof(T));
59  * }
60  *
61  * Most C++ types are relocatable; the ones that aren't would include
62  * internal pointers or (very rarely) would need to update remote
63  * pointers to pointers tracking them. All C++ primitive types and
64  * type constructors are relocatable.
65  *
66  * This property can be used in a variety of optimizations. Currently
67  * fbvector uses this property intensively.
68  *
69  * The default conservatively assumes the type is not
70  * relocatable. Several specializations are defined for known
71  * types. You may want to add your own specializations. Do so in
72  * namespace folly and make sure you keep the specialization of
73  * IsRelocatable<SomeStruct> in the same header as SomeStruct.
74  *
75  * You may also declare a type to be relocatable by including
76  *    `typedef std::true_type IsRelocatable;`
77  * in the class header.
78  *
79  * It may be unset in a base class by overriding the typedef to false_type.
80  */
81 /*
82  * IsTriviallyCopyable describes the value semantics property. C++11 contains
83  * the type trait is_trivially_copyable; however, it is not yet implemented
84  * in gcc (as of 4.7.1), and the user may wish to specify otherwise.
85  */
86 /*
87  * IsZeroInitializable describes the property that default construction is the
88  * same as memset(dst, 0, sizeof(T)).
89  */
90
91 namespace traits_detail {
92
93 #define FOLLY_HAS_TRUE_XXX(name)                                             \
94   BOOST_MPL_HAS_XXX_TRAIT_DEF(name)                                          \
95   template <class T>                                                         \
96   struct name##_is_true : std::is_same<typename T::name, std::true_type> {}; \
97   template <class T>                                                         \
98   struct has_true_##name : std::conditional<                                 \
99                                has_##name<T>::value,                         \
100                                name##_is_true<T>,                            \
101                                std::false_type>::type {};
102
103 FOLLY_HAS_TRUE_XXX(IsRelocatable)
104 FOLLY_HAS_TRUE_XXX(IsZeroInitializable)
105 FOLLY_HAS_TRUE_XXX(IsTriviallyCopyable)
106
107 #undef FOLLY_HAS_TRUE_XXX
108
109 // Older versions of libstdc++ do not provide std::is_trivially_copyable
110 #if defined(__clang__) && !defined(_LIBCPP_VERSION)
111 template <class T>
112 struct is_trivially_copyable
113     : std::integral_constant<bool, __is_trivially_copyable(T)> {};
114 #elif defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
115 template <class T>
116 struct is_trivially_copyable : std::is_trivial<T> {};
117 #else
118 template <class T>
119 using is_trivially_copyable = std::is_trivially_copyable<T>;
120 #endif
121 }
122
123 struct Ignore {
124   template <class T>
125   /* implicit */ Ignore(const T&) {}
126   template <class T>
127   const Ignore& operator=(T const&) const { return *this; }
128 };
129
130 template <class...>
131 using Ignored = Ignore;
132
133 namespace traits_detail_IsEqualityComparable {
134 Ignore operator==(Ignore, Ignore);
135
136 template <class T, class U = T>
137 struct IsEqualityComparable
138     : std::is_convertible<
139           decltype(std::declval<T>() == std::declval<U>()),
140           bool
141       > {};
142 }
143
144 /* using override */ using traits_detail_IsEqualityComparable::
145     IsEqualityComparable;
146
147 namespace traits_detail_IsLessThanComparable {
148 Ignore operator<(Ignore, Ignore);
149
150 template <class T, class U = T>
151 struct IsLessThanComparable
152     : std::is_convertible<
153           decltype(std::declval<T>() < std::declval<U>()),
154           bool
155       > {};
156 }
157
158 /* using override */ using traits_detail_IsLessThanComparable::
159     IsLessThanComparable;
160
161 namespace traits_detail_IsNothrowSwappable {
162 #if defined(_MSC_VER) || defined(__cpp_lib_is_swappable)
163 // MSVC already implements the C++17 P0185R1 proposal which
164 // adds std::is_nothrow_swappable, so use it instead.
165 template <typename T>
166 using IsNothrowSwappable = std::is_nothrow_swappable<T>;
167 #else
168 /* using override */ using std::swap;
169
170 template <class T>
171 struct IsNothrowSwappable
172     : std::integral_constant<bool,
173         std::is_nothrow_move_constructible<T>::value &&
174         noexcept(swap(std::declval<T&>(), std::declval<T&>()))
175       > {};
176 #endif
177 }
178
179 /* using override */ using traits_detail_IsNothrowSwappable::IsNothrowSwappable;
180
181 template <class T> struct IsTriviallyCopyable
182   : std::conditional<
183       traits_detail::has_IsTriviallyCopyable<T>::value,
184       traits_detail::has_true_IsTriviallyCopyable<T>,
185       traits_detail::is_trivially_copyable<T>
186     >::type {};
187
188 template <class T> struct IsRelocatable
189   : std::conditional<
190       traits_detail::has_IsRelocatable<T>::value,
191       traits_detail::has_true_IsRelocatable<T>,
192       // TODO add this line (and some tests for it) when we upgrade to gcc 4.7
193       //std::is_trivially_move_constructible<T>::value ||
194       IsTriviallyCopyable<T>
195     >::type {};
196
197 template <class T> struct IsZeroInitializable
198   : std::conditional<
199       traits_detail::has_IsZeroInitializable<T>::value,
200       traits_detail::has_true_IsZeroInitializable<T>,
201       std::integral_constant<bool, !std::is_class<T>::value>
202     >::type {};
203
204 template <typename...>
205 struct Conjunction : std::true_type {};
206 template <typename T>
207 struct Conjunction<T> : T {};
208 template <typename T, typename... TList>
209 struct Conjunction<T, TList...>
210     : std::conditional<T::value, Conjunction<TList...>, T>::type {};
211
212 template <typename...>
213 struct Disjunction : std::false_type {};
214 template <typename T>
215 struct Disjunction<T> : T {};
216 template <typename T, typename... TList>
217 struct Disjunction<T, TList...>
218     : std::conditional<T::value, T, Disjunction<TList...>>::type {};
219
220 template <typename T>
221 struct Negation : std::integral_constant<bool, !T::value> {};
222
223 template <bool... Bs>
224 struct Bools {
225   using valid_type = bool;
226   static constexpr std::size_t size() {
227     return sizeof...(Bs);
228   }
229 };
230
231 // Lighter-weight than Conjunction, but evaluates all sub-conditions eagerly.
232 template <class... Ts>
233 using StrictConjunction =
234     std::is_same<Bools<Ts::value..., true>, Bools<true, Ts::value...>>;
235
236 } // namespace folly
237
238 /**
239  * Use this macro ONLY inside namespace folly. When using it with a
240  * regular type, use it like this:
241  *
242  * // Make sure you're at namespace ::folly scope
243  * template<> FOLLY_ASSUME_RELOCATABLE(MyType)
244  *
245  * When using it with a template type, use it like this:
246  *
247  * // Make sure you're at namespace ::folly scope
248  * template<class T1, class T2>
249  * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
250  */
251 #define FOLLY_ASSUME_RELOCATABLE(...) \
252   struct IsRelocatable<  __VA_ARGS__ > : std::true_type {};
253
254 /**
255  * Use this macro ONLY inside namespace boost. When using it with a
256  * regular type, use it like this:
257  *
258  * // Make sure you're at namespace ::boost scope
259  * template<> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType)
260  *
261  * When using it with a template type, use it like this:
262  *
263  * // Make sure you're at namespace ::boost scope
264  * template<class T1, class T2>
265  * FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType<T1, T2>)
266  */
267 #define FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(...) \
268   struct has_nothrow_constructor<  __VA_ARGS__ > : ::boost::true_type {};
269
270 /**
271  * The FOLLY_ASSUME_FBVECTOR_COMPATIBLE* macros below encode two
272  * assumptions: first, that the type is relocatable per IsRelocatable
273  * above, and that it has a nothrow constructor. Most types can be
274  * assumed to satisfy both conditions, but it is the responsibility of
275  * the user to state that assumption. User-defined classes will not
276  * work with fbvector (see FBVector.h) unless they state this
277  * combination of properties.
278  *
279  * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
280  *
281  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
282  *
283  * The versions FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1, _2, _3, and _4
284  * allow using the macro for describing templatized classes with 1, 2,
285  * 3, and 4 template parameters respectively. For template classes
286  * just use the macro with the appropriate number and pass the name of
287  * the template to it. Example:
288  *
289  * template <class T1, class T2> class MyType { ... };
290  * ...
291  * // Make sure you're at global scope
292  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
293  */
294
295 // Use this macro ONLY at global level (no namespace)
296 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE(...)                           \
297   namespace folly { template<> FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__) }   \
298   namespace boost { \
299   template<> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__) }
300 // Use this macro ONLY at global level (no namespace)
301 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(...)                         \
302   namespace folly {                                                     \
303   template <class T1> FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>) }       \
304     namespace boost {                                                   \
305     template <class T1> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1>) }
306 // Use this macro ONLY at global level (no namespace)
307 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(...)                 \
308   namespace folly {                                             \
309   template <class T1, class T2>                                 \
310   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) }               \
311     namespace boost {                                           \
312     template <class T1, class T2>                               \
313     FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2>) }
314 // Use this macro ONLY at global level (no namespace)
315 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(...)                         \
316   namespace folly {                                                     \
317   template <class T1, class T2, class T3>                               \
318   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) }                   \
319     namespace boost {                                                   \
320     template <class T1, class T2, class T3>                             \
321     FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2, T3>) }
322 // Use this macro ONLY at global level (no namespace)
323 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(...)                         \
324   namespace folly {                                                     \
325   template <class T1, class T2, class T3, class T4>                     \
326   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) }               \
327     namespace boost {                                                   \
328     template <class T1, class T2, class T3, class T4>                   \
329     FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2, T3, T4>) }
330
331 /**
332  * Instantiate FOLLY_ASSUME_FBVECTOR_COMPATIBLE for a few types. It is
333  * safe to assume that pair is compatible if both of its components
334  * are. Furthermore, all STL containers can be assumed to comply,
335  * although that is not guaranteed by the standard.
336  */
337
338 FOLLY_NAMESPACE_STD_BEGIN
339
340 template <class T, class U>
341   struct pair;
342 #ifndef _GLIBCXX_USE_FB
343 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
344 template <class T, class R, class A>
345   class basic_string;
346 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
347 #else
348 template <class T, class R, class A, class S>
349   class basic_string;
350 #endif
351 template <class T, class A>
352   class vector;
353 template <class T, class A>
354   class deque;
355 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
356 template <class T, class A>
357   class list;
358 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
359 template <class T, class C, class A>
360   class set;
361 template <class K, class V, class C, class A>
362   class map;
363 template <class T>
364   class shared_ptr;
365
366 FOLLY_NAMESPACE_STD_END
367
368 namespace boost {
369
370 template <class T> class shared_ptr;
371
372 template <class T, class U>
373 struct has_nothrow_constructor< std::pair<T, U> >
374     : std::integral_constant<bool,
375         has_nothrow_constructor<T>::value &&
376         has_nothrow_constructor<U>::value> {};
377
378 } // namespace boost
379
380 namespace folly {
381
382 // STL commonly-used types
383 template <class T, class U>
384 struct IsRelocatable< std::pair<T, U> >
385     : std::integral_constant<bool,
386         IsRelocatable<T>::value &&
387         IsRelocatable<U>::value> {};
388
389 // Is T one of T1, T2, ..., Tn?
390 template <class T, class... Ts>
391 struct IsOneOf {
392   enum { value = false };
393 };
394
395 template <class T, class T1, class... Ts>
396 struct IsOneOf<T, T1, Ts...> {
397   enum { value = std::is_same<T, T1>::value || IsOneOf<T, Ts...>::value };
398 };
399
400 /*
401  * Complementary type traits for integral comparisons.
402  *
403  * For instance, `if(x < 0)` yields an error in clang for unsigned types
404  *  when -Werror is used due to -Wtautological-compare
405  *
406  *
407  * @author: Marcelo Juchem <marcelo@fb.com>
408  */
409
410 namespace detail {
411
412 template <typename T, bool>
413 struct is_negative_impl {
414   constexpr static bool check(T x) { return x < 0; }
415 };
416
417 template <typename T>
418 struct is_negative_impl<T, false> {
419   constexpr static bool check(T) { return false; }
420 };
421
422 // folly::to integral specializations can end up generating code
423 // inside what are really static ifs (not executed because of the templated
424 // types) that violate -Wsign-compare and/or -Wbool-compare so suppress them
425 // in order to not prevent all calling code from using it.
426 #pragma GCC diagnostic push
427 #pragma GCC diagnostic ignored "-Wsign-compare"
428 #if __GNUC_PREREQ(5, 0)
429 #pragma GCC diagnostic ignored "-Wbool-compare"
430 #endif
431
432 template <typename RHS, RHS rhs, typename LHS>
433 bool less_than_impl(LHS const lhs) {
434   return
435     rhs > std::numeric_limits<LHS>::max() ? true :
436     rhs <= std::numeric_limits<LHS>::min() ? false :
437     lhs < rhs;
438 }
439
440 template <typename RHS, RHS rhs, typename LHS>
441 bool greater_than_impl(LHS const lhs) {
442   return
443     rhs > std::numeric_limits<LHS>::max() ? false :
444     rhs < std::numeric_limits<LHS>::min() ? true :
445     lhs > rhs;
446 }
447
448 #pragma GCC diagnostic pop
449
450 } // namespace detail {
451
452 // same as `x < 0`
453 template <typename T>
454 constexpr bool is_negative(T x) {
455   return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x);
456 }
457
458 // same as `x <= 0`
459 template <typename T>
460 constexpr bool is_non_positive(T x) { return !x || folly::is_negative(x); }
461
462 // same as `x > 0`
463 template <typename T>
464 constexpr bool is_positive(T x) { return !is_non_positive(x); }
465
466 // same as `x >= 0`
467 template <typename T>
468 constexpr bool is_non_negative(T x) {
469   return !x || is_positive(x);
470 }
471
472 template <typename RHS, RHS rhs, typename LHS>
473 bool less_than(LHS const lhs) {
474   return detail::less_than_impl<
475     RHS, rhs, typename std::remove_reference<LHS>::type
476   >(lhs);
477 }
478
479 template <typename RHS, RHS rhs, typename LHS>
480 bool greater_than(LHS const lhs) {
481   return detail::greater_than_impl<
482     RHS, rhs, typename std::remove_reference<LHS>::type
483   >(lhs);
484 }
485
486 namespace traits_detail {
487 struct InPlaceTag {};
488 template <class>
489 struct InPlaceTypeTag {};
490 template <std::size_t>
491 struct InPlaceIndexTag {};
492 }
493
494 /**
495  * Like std::piecewise_construct, a tag type & instance used for in-place
496  * construction of non-movable contained types, e.g. by Synchronized.
497  * Follows the naming and design of std::in_place suggested in
498  * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0032r2.pdf
499  */
500 using in_place_t = traits_detail::InPlaceTag (&)(traits_detail::InPlaceTag);
501
502 template <class T>
503 using in_place_type_t =
504     traits_detail::InPlaceTypeTag<T> (&)(traits_detail::InPlaceTypeTag<T>);
505
506 template <std::size_t I>
507 using in_place_index_t =
508     traits_detail::InPlaceIndexTag<I> (&)(traits_detail::InPlaceIndexTag<I>);
509
510 inline traits_detail::InPlaceTag in_place(traits_detail::InPlaceTag = {}) {
511   return {};
512 }
513
514 template <class T>
515 inline traits_detail::InPlaceTypeTag<T> in_place(
516     traits_detail::InPlaceTypeTag<T> = {}) {
517   return {};
518 }
519
520 template <std::size_t I>
521 inline traits_detail::InPlaceIndexTag<I> in_place(
522     traits_detail::InPlaceIndexTag<I> = {}) {
523   return {};
524 }
525
526 // For backwards compatibility:
527 using construct_in_place_t = in_place_t;
528
529 inline traits_detail::InPlaceTag construct_in_place(
530     traits_detail::InPlaceTag = {}) {
531   return {};
532 }
533
534 /**
535  * Initializer lists are a powerful compile time syntax introduced in C++11
536  * but due to their often conflicting syntax they are not used by APIs for
537  * construction.
538  *
539  * Further standard conforming compilers *strongly* favor an
540  * std::initalizer_list overload for construction if one exists.  The
541  * following is a simple tag used to disambiguate construction with
542  * initializer lists and regular uniform initialization.
543  *
544  * For example consider the following case
545  *
546  *  class Something {
547  *  public:
548  *    explicit Something(int);
549  *    Something(std::intiializer_list<int>);
550  *
551  *    operator int();
552  *  };
553  *
554  *  ...
555  *  Something something{1}; // SURPRISE!!
556  *
557  * The last call to instantiate the Something object will go to the
558  * initializer_list overload.  Which may be surprising to users.
559  *
560  * If however this tag was used to disambiguate such construction it would be
561  * easy for users to see which construction overload their code was referring
562  * to.  For example
563  *
564  *  class Something {
565  *  public:
566  *    explicit Something(int);
567  *    Something(folly::initlist_construct_t, std::initializer_list<int>);
568  *
569  *    operator int();
570  *  };
571  *
572  *  ...
573  *  Something something_one{1}; // not the initializer_list overload
574  *  Something something_two{folly::initlist_construct, {1}}; // correct
575  */
576 struct initlist_construct_t {};
577 constexpr initlist_construct_t initlist_construct{};
578
579 } // namespace folly
580
581 // gcc-5.0 changed string's implementation in libgcc to be non-relocatable
582 #if __GNUC__ < 5
583 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string)
584 #endif
585 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector)
586 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::list)
587 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque)
588 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr)
589 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr)
590 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function)
591
592 // Boost
593 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(boost::shared_ptr)
594
595 #define FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(classname, type_name) \
596   template <typename T> \
597   struct classname { \
598     template <typename C> \
599     constexpr static bool test(typename C::type_name*) { return true; } \
600     template <typename> \
601     constexpr static bool test(...) { return false; } \
602     constexpr static bool value = test<T>(nullptr); \
603   }
604
605 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, cv_qual) \
606   template <typename TTheClass_, typename RTheReturn_, typename... TTheArgs_> \
607   class classname<TTheClass_, RTheReturn_(TTheArgs_...) cv_qual> { \
608     template < \
609       typename UTheClass_, RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual \
610     > struct sfinae {}; \
611     template <typename UTheClass_> \
612     constexpr static bool test(sfinae<UTheClass_, &UTheClass_::func_name>*) \
613     { return true; } \
614     template <typename> \
615     constexpr static bool test(...) { return false; } \
616   public: \
617     constexpr static bool value = test<TTheClass_>(nullptr); \
618   }
619
620 /*
621  * The FOLLY_CREATE_HAS_MEMBER_FN_TRAITS is used to create traits
622  * classes that check for the existence of a member function with
623  * a given name and signature. It currently does not support
624  * checking for inherited members.
625  *
626  * Such classes receive two template parameters: the class to be checked
627  * and the signature of the member function. A static boolean field
628  * named `value` (which is also constexpr) tells whether such member
629  * function exists.
630  *
631  * Each traits class created is bound only to the member name, not to
632  * its signature nor to the type of the class containing it.
633  *
634  * Say you need to know if a given class has a member function named
635  * `test` with the following signature:
636  *
637  *    int test() const;
638  *
639  * You'd need this macro to create a traits class to check for a member
640  * named `test`, and then use this traits class to check for the signature:
641  *
642  * namespace {
643  *
644  * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
645  *
646  * } // unnamed-namespace
647  *
648  * void some_func() {
649  *   cout << "Does class Foo have a member int test() const? "
650  *     << boolalpha << has_test_traits<Foo, int() const>::value;
651  * }
652  *
653  * You can use the same traits class to test for a completely different
654  * signature, on a completely different class, as long as the member name
655  * is the same:
656  *
657  * void some_func() {
658  *   cout << "Does class Foo have a member int test()? "
659  *     << boolalpha << has_test_traits<Foo, int()>::value;
660  *   cout << "Does class Foo have a member int test() const? "
661  *     << boolalpha << has_test_traits<Foo, int() const>::value;
662  *   cout << "Does class Bar have a member double test(const string&, long)? "
663  *     << boolalpha << has_test_traits<Bar, double(const string&, long)>::value;
664  * }
665  *
666  * @author: Marcelo Juchem <marcelo@fb.com>
667  */
668 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(classname, func_name) \
669   template <typename, typename> class classname; \
670   FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, ); \
671   FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, const); \
672   FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
673       classname, func_name, /* nolint */ volatile); \
674   FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL( \
675       classname, func_name, /* nolint */ volatile const)
676
677 /* Some combinations of compilers and C++ libraries make __int128 and
678  * unsigned __int128 available but do not correctly define their standard type
679  * traits.
680  *
681  * If FOLLY_SUPPLY_MISSING_INT128_TRAITS is defined, we define these traits
682  * here.
683  *
684  * @author: Phil Willoughby <philwill@fb.com>
685  */
686 #if FOLLY_SUPPLY_MISSING_INT128_TRAITS
687 FOLLY_NAMESPACE_STD_BEGIN
688 template <>
689 struct is_arithmetic<__int128> : ::std::true_type {};
690 template <>
691 struct is_arithmetic<unsigned __int128> : ::std::true_type {};
692 template <>
693 struct is_integral<__int128> : ::std::true_type {};
694 template <>
695 struct is_integral<unsigned __int128> : ::std::true_type {};
696 template <>
697 struct make_unsigned<__int128> {
698   typedef unsigned __int128 type;
699 };
700 template <>
701 struct make_signed<__int128> {
702   typedef __int128 type;
703 };
704 template <>
705 struct make_unsigned<unsigned __int128> {
706   typedef unsigned __int128 type;
707 };
708 template <>
709 struct make_signed<unsigned __int128> {
710   typedef __int128 type;
711 };
712 template <>
713 struct is_signed<__int128> : ::std::true_type {};
714 template <>
715 struct is_unsigned<unsigned __int128> : ::std::true_type {};
716 FOLLY_NAMESPACE_STD_END
717 #endif // FOLLY_SUPPLY_MISSING_INT128_TRAITS