Support compiling in C++14 mode
[folly.git] / folly / Traits.h
1 /*
2  * Copyright 2017 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 #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*);               \
44     template <typename>                                                        \
45     static std::false_type test(...);                                          \
46   };                                                                           \
47   template <typename TTheClass_>                                               \
48   using classname = decltype(                                                  \
49       classname##__folly_traits_impl__<TTheClass_>::template test<TTheClass_>( \
50           nullptr))
51
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__<                                    \
55       TTheClass_,                                                             \
56       RTheReturn_(TTheArgs_...) cv_qual> {                                    \
57     template <                                                                \
58         typename UTheClass_,                                                  \
59         RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual>                    \
60     struct sfinae {};                                                         \
61     template <typename UTheClass_>                                            \
62     static std::true_type test(sfinae<UTheClass_, &UTheClass_::func_name>*);  \
63     template <typename>                                                       \
64     static std::false_type test(...);                                         \
65   }
66
67 /*
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.
72  *
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
76  * function exists.
77  *
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.
80  *
81  * Say you need to know if a given class has a member function named
82  * `test` with the following signature:
83  *
84  *    int test() const;
85  *
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:
88  *
89  * namespace {
90  *
91  * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
92  *
93  * } // unnamed-namespace
94  *
95  * void some_func() {
96  *   cout << "Does class Foo have a member int test() const? "
97  *     << boolalpha << has_test_traits<Foo, int() const>::value;
98  * }
99  *
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
102  * is the same:
103  *
104  * void some_func() {
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;
111  * }
112  *
113  * @author: Marcelo Juchem <marcelo@fb.com>
114  */
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_>                     \
125   using classname =                                                           \
126       decltype(classname##__folly_traits_impl__<TTheClass_, TTheSignature_>:: \
127                    template test<TTheClass_>(nullptr))
128
129 namespace folly {
130
131 /***
132  *  _t
133  *
134  *  Instead of:
135  *
136  *    using decayed = typename std::decay<T>::type;
137  *
138  *  With the C++14 standard trait aliases, we could use:
139  *
140  *    using decayed = std::decay_t<T>;
141  *
142  *  Without them, we could use:
143  *
144  *    using decayed = _t<std::decay<T>>;
145  *
146  *  Also useful for any other library with template types having dependent
147  *  member types named `type`, like the standard trait types.
148  */
149 template <typename T>
150 using _t = typename T::type;
151
152 /**
153  *  void_t
154  *
155  *  A type alias for `void`. `void_t` is useful for controling class-template
156  *  partial specialization.
157  *
158  *  Example:
159  *
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 {};
164  *
165  *    template <class T>
166  *    struct has_value_type<T, folly::void_t<typename T::value_type>>
167  *        : std::true_type {};
168  */
169 #if defined(__cpp_lib_void_t) || defined(_MSC_VER)
170
171 /* using override */ using std::void_t;
172
173 #else // defined(__cpp_lib_void_t) || defined(_MSC_VER)
174
175 namespace traits_detail {
176 template <class...>
177 struct void_t_ {
178   using type = void;
179 };
180 } // namespace traits_detail
181
182 template <class... Ts>
183 using void_t = _t<traits_detail::void_t_<Ts...>>;
184
185 #endif // defined(__cpp_lib_void_t) || defined(_MSC_VER)
186
187 /**
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
193  * equivalent:
194  *
195  * void move1(T * from, T * to) {
196  *   new(to) T(from);
197  *   (*from).~T();
198  * }
199  *
200  * void move2(T * from, T * to) {
201  *   memcpy(to, from, sizeof(T));
202  * }
203  *
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.
208  *
209  * This property can be used in a variety of optimizations. Currently
210  * fbvector uses this property intensively.
211  *
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.
217  *
218  * You may also declare a type to be relocatable by including
219  *    `typedef std::true_type IsRelocatable;`
220  * in the class header.
221  *
222  * It may be unset in a base class by overriding the typedef to false_type.
223  */
224 /*
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.
228  */
229 /*
230  * IsZeroInitializable describes the property that default construction is the
231  * same as memset(dst, 0, sizeof(T)).
232  */
233
234 namespace traits_detail {
235
236 #define FOLLY_HAS_TRUE_XXX(name)                                             \
237   FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_##name, name);                     \
238   template <class T>                                                         \
239   struct name##_is_true : std::is_same<typename T::name, std::true_type> {}; \
240   template <class T>                                                         \
241   struct has_true_##name : std::conditional<                                 \
242                                has_##name<T>::value,                         \
243                                name##_is_true<T>,                            \
244                                std::false_type>::type {};
245
246 FOLLY_HAS_TRUE_XXX(IsRelocatable)
247 FOLLY_HAS_TRUE_XXX(IsZeroInitializable)
248 FOLLY_HAS_TRUE_XXX(IsTriviallyCopyable)
249
250 #undef FOLLY_HAS_TRUE_XXX
251
252 // Older versions of libstdc++ do not provide std::is_trivially_copyable
253 #if defined(__clang__) && !defined(_LIBCPP_VERSION)
254 template <class T>
255 struct is_trivially_copyable
256     : std::integral_constant<bool, __is_trivially_copyable(T)> {};
257 #elif defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
258 template <class T>
259 struct is_trivially_copyable : std::is_trivial<T> {};
260 #else
261 template <class T>
262 using is_trivially_copyable = std::is_trivially_copyable<T>;
263 #endif
264 }
265
266 struct Ignore {
267   template <class T>
268   /* implicit */ Ignore(const T&) {}
269   template <class T>
270   const Ignore& operator=(T const&) const { return *this; }
271 };
272
273 template <class...>
274 using Ignored = Ignore;
275
276 namespace traits_detail_IsEqualityComparable {
277 Ignore operator==(Ignore, Ignore);
278
279 template <class T, class U = T>
280 struct IsEqualityComparable
281     : std::is_convertible<
282           decltype(std::declval<T>() == std::declval<U>()),
283           bool
284       > {};
285 }
286
287 /* using override */ using traits_detail_IsEqualityComparable::
288     IsEqualityComparable;
289
290 namespace traits_detail_IsLessThanComparable {
291 Ignore operator<(Ignore, Ignore);
292
293 template <class T, class U = T>
294 struct IsLessThanComparable
295     : std::is_convertible<
296           decltype(std::declval<T>() < std::declval<U>()),
297           bool
298       > {};
299 }
300
301 /* using override */ using traits_detail_IsLessThanComparable::
302     IsLessThanComparable;
303
304 namespace traits_detail_IsNothrowSwappable {
305 #if defined(__cpp_lib_is_swappable) || (_CPPLIB_VER && _HAS_CXX17)
306 // MSVC 2015+ already implements the C++17 P0185R1 proposal which
307 // adds std::is_nothrow_swappable, so use it instead if C++17 mode
308 // is enabled.
309 template <typename T>
310 using IsNothrowSwappable = std::is_nothrow_swappable<T>;
311 #elif _CPPLIB_VER
312 // MSVC 2015+ defines the base even if C++17 is disabled, and
313 // MSVC 2015 has issues with our fallback implementation due to
314 // over-eager evaluation of noexcept.
315 template <typename T>
316 using IsNothrowSwappable = std::_Is_nothrow_swappable<T>;
317 #else
318 /* using override */ using std::swap;
319
320 template <class T>
321 struct IsNothrowSwappable
322     : std::integral_constant<bool,
323         std::is_nothrow_move_constructible<T>::value &&
324         noexcept(swap(std::declval<T&>(), std::declval<T&>()))
325       > {};
326 #endif
327 }
328
329 /* using override */ using traits_detail_IsNothrowSwappable::IsNothrowSwappable;
330
331 template <class T> struct IsTriviallyCopyable
332   : std::conditional<
333       traits_detail::has_IsTriviallyCopyable<T>::value,
334       traits_detail::has_true_IsTriviallyCopyable<T>,
335       traits_detail::is_trivially_copyable<T>
336     >::type {};
337
338 template <class T> struct IsRelocatable
339   : std::conditional<
340       traits_detail::has_IsRelocatable<T>::value,
341       traits_detail::has_true_IsRelocatable<T>,
342       // TODO add this line (and some tests for it) when we upgrade to gcc 4.7
343       //std::is_trivially_move_constructible<T>::value ||
344       IsTriviallyCopyable<T>
345     >::type {};
346
347 template <class T> struct IsZeroInitializable
348   : std::conditional<
349       traits_detail::has_IsZeroInitializable<T>::value,
350       traits_detail::has_true_IsZeroInitializable<T>,
351       std::integral_constant<bool, !std::is_class<T>::value>
352     >::type {};
353
354 template <typename...>
355 struct Conjunction : std::true_type {};
356 template <typename T>
357 struct Conjunction<T> : T {};
358 template <typename T, typename... TList>
359 struct Conjunction<T, TList...>
360     : std::conditional<T::value, Conjunction<TList...>, T>::type {};
361
362 template <typename...>
363 struct Disjunction : std::false_type {};
364 template <typename T>
365 struct Disjunction<T> : T {};
366 template <typename T, typename... TList>
367 struct Disjunction<T, TList...>
368     : std::conditional<T::value, T, Disjunction<TList...>>::type {};
369
370 template <typename T>
371 struct Negation : std::integral_constant<bool, !T::value> {};
372
373 template <bool... Bs>
374 struct Bools {
375   using valid_type = bool;
376   static constexpr std::size_t size() {
377     return sizeof...(Bs);
378   }
379 };
380
381 // Lighter-weight than Conjunction, but evaluates all sub-conditions eagerly.
382 template <class... Ts>
383 using StrictConjunction =
384     std::is_same<Bools<Ts::value..., true>, Bools<true, Ts::value...>>;
385
386 } // namespace folly
387
388 /**
389  * Use this macro ONLY inside namespace folly. When using it with a
390  * regular type, use it like this:
391  *
392  * // Make sure you're at namespace ::folly scope
393  * template<> FOLLY_ASSUME_RELOCATABLE(MyType)
394  *
395  * When using it with a template type, use it like this:
396  *
397  * // Make sure you're at namespace ::folly scope
398  * template<class T1, class T2>
399  * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
400  */
401 #define FOLLY_ASSUME_RELOCATABLE(...) \
402   struct IsRelocatable<  __VA_ARGS__ > : std::true_type {};
403
404 /**
405  * The FOLLY_ASSUME_FBVECTOR_COMPATIBLE* macros below encode the
406  * assumption that the type is relocatable per IsRelocatable
407  * above. Many types can be assumed to satisfy this condition, but
408  * it is the responsibility of the user to state that assumption.
409  * User-defined classes will not be optimized for use with
410  * fbvector (see FBVector.h) unless they state that assumption.
411  *
412  * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
413  *
414  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
415  *
416  * The versions FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1, _2, _3, and _4
417  * allow using the macro for describing templatized classes with 1, 2,
418  * 3, and 4 template parameters respectively. For template classes
419  * just use the macro with the appropriate number and pass the name of
420  * the template to it. Example:
421  *
422  * template <class T1, class T2> class MyType { ... };
423  * ...
424  * // Make sure you're at global scope
425  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
426  */
427
428 // Use this macro ONLY at global level (no namespace)
429 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE(...) \
430   namespace folly {                           \
431   template <>                                 \
432   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__)       \
433   }
434 // Use this macro ONLY at global level (no namespace)
435 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(...) \
436   namespace folly {                             \
437   template <class T1>                           \
438   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>)     \
439   }
440 // Use this macro ONLY at global level (no namespace)
441 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(...) \
442   namespace folly {                             \
443   template <class T1, class T2>                 \
444   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) \
445   }
446 // Use this macro ONLY at global level (no namespace)
447 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(...)     \
448   namespace folly {                                 \
449   template <class T1, class T2, class T3>           \
450   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) \
451   }
452 // Use this macro ONLY at global level (no namespace)
453 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(...)         \
454   namespace folly {                                     \
455   template <class T1, class T2, class T3, class T4>     \
456   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) \
457   }
458
459 /**
460  * Instantiate FOLLY_ASSUME_FBVECTOR_COMPATIBLE for a few types. It is
461  * safe to assume that pair is compatible if both of its components
462  * are. Furthermore, all STL containers can be assumed to comply,
463  * although that is not guaranteed by the standard.
464  */
465
466 FOLLY_NAMESPACE_STD_BEGIN
467
468 template <class T, class U>
469   struct pair;
470 #ifndef _GLIBCXX_USE_FB
471 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
472 template <class T, class R, class A>
473   class basic_string;
474 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
475 #else
476 template <class T, class R, class A, class S>
477   class basic_string;
478 #endif
479 template <class T, class A>
480   class vector;
481 template <class T, class A>
482   class deque;
483 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
484 template <class T, class A>
485   class list;
486 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
487 template <class T, class C, class A>
488   class set;
489 template <class K, class V, class C, class A>
490   class map;
491 template <class T>
492   class shared_ptr;
493
494 FOLLY_NAMESPACE_STD_END
495
496 namespace folly {
497
498 // STL commonly-used types
499 template <class T, class U>
500 struct IsRelocatable< std::pair<T, U> >
501     : std::integral_constant<bool,
502         IsRelocatable<T>::value &&
503         IsRelocatable<U>::value> {};
504
505 // Is T one of T1, T2, ..., Tn?
506 template <class T, class... Ts>
507 struct IsOneOf {
508   enum { value = false };
509 };
510
511 template <class T, class T1, class... Ts>
512 struct IsOneOf<T, T1, Ts...> {
513   enum { value = std::is_same<T, T1>::value || IsOneOf<T, Ts...>::value };
514 };
515
516 /*
517  * Complementary type traits for integral comparisons.
518  *
519  * For instance, `if(x < 0)` yields an error in clang for unsigned types
520  *  when -Werror is used due to -Wtautological-compare
521  *
522  *
523  * @author: Marcelo Juchem <marcelo@fb.com>
524  */
525
526 namespace detail {
527
528 template <typename T, bool>
529 struct is_negative_impl {
530   constexpr static bool check(T x) { return x < 0; }
531 };
532
533 template <typename T>
534 struct is_negative_impl<T, false> {
535   constexpr static bool check(T) { return false; }
536 };
537
538 // folly::to integral specializations can end up generating code
539 // inside what are really static ifs (not executed because of the templated
540 // types) that violate -Wsign-compare and/or -Wbool-compare so suppress them
541 // in order to not prevent all calling code from using it.
542 FOLLY_PUSH_WARNING
543 FOLLY_GCC_DISABLE_WARNING(sign-compare)
544 #if __GNUC_PREREQ(5, 0)
545 FOLLY_GCC_DISABLE_WARNING(bool-compare)
546 #endif
547 FOLLY_MSVC_DISABLE_WARNING(4388) // sign-compare
548 FOLLY_MSVC_DISABLE_WARNING(4804) // bool-compare
549
550 template <typename RHS, RHS rhs, typename LHS>
551 bool less_than_impl(LHS const lhs) {
552   return
553     rhs > std::numeric_limits<LHS>::max() ? true :
554     rhs <= std::numeric_limits<LHS>::min() ? false :
555     lhs < rhs;
556 }
557
558 template <typename RHS, RHS rhs, typename LHS>
559 bool greater_than_impl(LHS const lhs) {
560   return
561     rhs > std::numeric_limits<LHS>::max() ? false :
562     rhs < std::numeric_limits<LHS>::min() ? true :
563     lhs > rhs;
564 }
565
566 FOLLY_POP_WARNING
567
568 } // namespace detail {
569
570 // same as `x < 0`
571 template <typename T>
572 constexpr bool is_negative(T x) {
573   return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x);
574 }
575
576 // same as `x <= 0`
577 template <typename T>
578 constexpr bool is_non_positive(T x) { return !x || folly::is_negative(x); }
579
580 // same as `x > 0`
581 template <typename T>
582 constexpr bool is_positive(T x) { return !is_non_positive(x); }
583
584 // same as `x >= 0`
585 template <typename T>
586 constexpr bool is_non_negative(T x) {
587   return !x || is_positive(x);
588 }
589
590 template <typename RHS, RHS rhs, typename LHS>
591 bool less_than(LHS const lhs) {
592   return detail::less_than_impl<
593     RHS, rhs, typename std::remove_reference<LHS>::type
594   >(lhs);
595 }
596
597 template <typename RHS, RHS rhs, typename LHS>
598 bool greater_than(LHS const lhs) {
599   return detail::greater_than_impl<
600     RHS, rhs, typename std::remove_reference<LHS>::type
601   >(lhs);
602 }
603
604 namespace traits_detail {
605 struct InPlaceTag {};
606 template <class>
607 struct InPlaceTypeTag {};
608 template <std::size_t>
609 struct InPlaceIndexTag {};
610 }
611
612 /**
613  * Like std::piecewise_construct, a tag type & instance used for in-place
614  * construction of non-movable contained types, e.g. by Synchronized.
615  * Follows the naming and design of std::in_place suggested in
616  * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0032r2.pdf
617  */
618 using in_place_t = traits_detail::InPlaceTag (&)(traits_detail::InPlaceTag);
619
620 template <class T>
621 using in_place_type_t =
622     traits_detail::InPlaceTypeTag<T> (&)(traits_detail::InPlaceTypeTag<T>);
623
624 template <std::size_t I>
625 using in_place_index_t =
626     traits_detail::InPlaceIndexTag<I> (&)(traits_detail::InPlaceIndexTag<I>);
627
628 inline traits_detail::InPlaceTag in_place(traits_detail::InPlaceTag = {}) {
629   return {};
630 }
631
632 template <class T>
633 inline traits_detail::InPlaceTypeTag<T> in_place(
634     traits_detail::InPlaceTypeTag<T> = {}) {
635   return {};
636 }
637
638 template <std::size_t I>
639 inline traits_detail::InPlaceIndexTag<I> in_place(
640     traits_detail::InPlaceIndexTag<I> = {}) {
641   return {};
642 }
643
644 // For backwards compatibility:
645 using construct_in_place_t = in_place_t;
646
647 inline traits_detail::InPlaceTag construct_in_place(
648     traits_detail::InPlaceTag = {}) {
649   return {};
650 }
651
652 /**
653  * Initializer lists are a powerful compile time syntax introduced in C++11
654  * but due to their often conflicting syntax they are not used by APIs for
655  * construction.
656  *
657  * Further standard conforming compilers *strongly* favor an
658  * std::initalizer_list overload for construction if one exists.  The
659  * following is a simple tag used to disambiguate construction with
660  * initializer lists and regular uniform initialization.
661  *
662  * For example consider the following case
663  *
664  *  class Something {
665  *  public:
666  *    explicit Something(int);
667  *    Something(std::intiializer_list<int>);
668  *
669  *    operator int();
670  *  };
671  *
672  *  ...
673  *  Something something{1}; // SURPRISE!!
674  *
675  * The last call to instantiate the Something object will go to the
676  * initializer_list overload.  Which may be surprising to users.
677  *
678  * If however this tag was used to disambiguate such construction it would be
679  * easy for users to see which construction overload their code was referring
680  * to.  For example
681  *
682  *  class Something {
683  *  public:
684  *    explicit Something(int);
685  *    Something(folly::initlist_construct_t, std::initializer_list<int>);
686  *
687  *    operator int();
688  *  };
689  *
690  *  ...
691  *  Something something_one{1}; // not the initializer_list overload
692  *  Something something_two{folly::initlist_construct, {1}}; // correct
693  */
694 struct initlist_construct_t {};
695 constexpr initlist_construct_t initlist_construct{};
696
697 } // namespace folly
698
699 // Assume nothing when compiling with MSVC.
700 #ifndef _MSC_VER
701 // gcc-5.0 changed string's implementation in libstdc++ to be non-relocatable
702 #if !_GLIBCXX_USE_CXX11_ABI
703 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string)
704 #endif
705 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector)
706 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::list)
707 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque)
708 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr)
709 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr)
710 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function)
711 #endif
712
713 /* Some combinations of compilers and C++ libraries make __int128 and
714  * unsigned __int128 available but do not correctly define their standard type
715  * traits.
716  *
717  * If FOLLY_SUPPLY_MISSING_INT128_TRAITS is defined, we define these traits
718  * here.
719  *
720  * @author: Phil Willoughby <philwill@fb.com>
721  */
722 #if FOLLY_SUPPLY_MISSING_INT128_TRAITS
723 FOLLY_NAMESPACE_STD_BEGIN
724 template <>
725 struct is_arithmetic<__int128> : ::std::true_type {};
726 template <>
727 struct is_arithmetic<unsigned __int128> : ::std::true_type {};
728 template <>
729 struct is_integral<__int128> : ::std::true_type {};
730 template <>
731 struct is_integral<unsigned __int128> : ::std::true_type {};
732 template <>
733 struct make_unsigned<__int128> {
734   typedef unsigned __int128 type;
735 };
736 template <>
737 struct make_signed<__int128> {
738   typedef __int128 type;
739 };
740 template <>
741 struct make_unsigned<unsigned __int128> {
742   typedef unsigned __int128 type;
743 };
744 template <>
745 struct make_signed<unsigned __int128> {
746   typedef __int128 type;
747 };
748 template <>
749 struct is_signed<__int128> : ::std::true_type {};
750 template <>
751 struct is_unsigned<unsigned __int128> : ::std::true_type {};
752 FOLLY_NAMESPACE_STD_END
753 #endif // FOLLY_SUPPLY_MISSING_INT128_TRAITS