folly/Traits.h: fix a deep problem with FBVector
[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(_MSC_VER) || defined(__cpp_lib_is_swappable)
306 // MSVC already implements the C++17 P0185R1 proposal which
307 // adds std::is_nothrow_swappable, so use it instead.
308 template <typename T>
309 using IsNothrowSwappable = std::is_nothrow_swappable<T>;
310 #else
311 /* using override */ using std::swap;
312
313 template <class T>
314 struct IsNothrowSwappable
315     : std::integral_constant<bool,
316         std::is_nothrow_move_constructible<T>::value &&
317         noexcept(swap(std::declval<T&>(), std::declval<T&>()))
318       > {};
319 #endif
320 }
321
322 /* using override */ using traits_detail_IsNothrowSwappable::IsNothrowSwappable;
323
324 template <class T> struct IsTriviallyCopyable
325   : std::conditional<
326       traits_detail::has_IsTriviallyCopyable<T>::value,
327       traits_detail::has_true_IsTriviallyCopyable<T>,
328       traits_detail::is_trivially_copyable<T>
329     >::type {};
330
331 template <class T> struct IsRelocatable
332   : std::conditional<
333       traits_detail::has_IsRelocatable<T>::value,
334       traits_detail::has_true_IsRelocatable<T>,
335       // TODO add this line (and some tests for it) when we upgrade to gcc 4.7
336       //std::is_trivially_move_constructible<T>::value ||
337       IsTriviallyCopyable<T>
338     >::type {};
339
340 template <class T> struct IsZeroInitializable
341   : std::conditional<
342       traits_detail::has_IsZeroInitializable<T>::value,
343       traits_detail::has_true_IsZeroInitializable<T>,
344       std::integral_constant<bool, !std::is_class<T>::value>
345     >::type {};
346
347 template <typename...>
348 struct Conjunction : std::true_type {};
349 template <typename T>
350 struct Conjunction<T> : T {};
351 template <typename T, typename... TList>
352 struct Conjunction<T, TList...>
353     : std::conditional<T::value, Conjunction<TList...>, T>::type {};
354
355 template <typename...>
356 struct Disjunction : std::false_type {};
357 template <typename T>
358 struct Disjunction<T> : T {};
359 template <typename T, typename... TList>
360 struct Disjunction<T, TList...>
361     : std::conditional<T::value, T, Disjunction<TList...>>::type {};
362
363 template <typename T>
364 struct Negation : std::integral_constant<bool, !T::value> {};
365
366 template <bool... Bs>
367 struct Bools {
368   using valid_type = bool;
369   static constexpr std::size_t size() {
370     return sizeof...(Bs);
371   }
372 };
373
374 // Lighter-weight than Conjunction, but evaluates all sub-conditions eagerly.
375 template <class... Ts>
376 using StrictConjunction =
377     std::is_same<Bools<Ts::value..., true>, Bools<true, Ts::value...>>;
378
379 } // namespace folly
380
381 /**
382  * Use this macro ONLY inside namespace folly. When using it with a
383  * regular type, use it like this:
384  *
385  * // Make sure you're at namespace ::folly scope
386  * template<> FOLLY_ASSUME_RELOCATABLE(MyType)
387  *
388  * When using it with a template type, use it like this:
389  *
390  * // Make sure you're at namespace ::folly scope
391  * template<class T1, class T2>
392  * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
393  */
394 #define FOLLY_ASSUME_RELOCATABLE(...) \
395   struct IsRelocatable<  __VA_ARGS__ > : std::true_type {};
396
397 /**
398  * The FOLLY_ASSUME_FBVECTOR_COMPATIBLE* macros below encode the
399  * assumption that the type is relocatable per IsRelocatable
400  * above. Many types can be assumed to satisfy this condition, but
401  * it is the responsibility of the user to state that assumption.
402  * User-defined classes will not be optimized for use with
403  * fbvector (see FBVector.h) unless they state that assumption.
404  *
405  * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
406  *
407  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
408  *
409  * The versions FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1, _2, _3, and _4
410  * allow using the macro for describing templatized classes with 1, 2,
411  * 3, and 4 template parameters respectively. For template classes
412  * just use the macro with the appropriate number and pass the name of
413  * the template to it. Example:
414  *
415  * template <class T1, class T2> class MyType { ... };
416  * ...
417  * // Make sure you're at global scope
418  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
419  */
420
421 // Use this macro ONLY at global level (no namespace)
422 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE(...) \
423   namespace folly {                           \
424   template <>                                 \
425   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__)       \
426   }
427 // Use this macro ONLY at global level (no namespace)
428 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(...) \
429   namespace folly {                             \
430   template <class T1>                           \
431   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>)     \
432   }
433 // Use this macro ONLY at global level (no namespace)
434 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(...) \
435   namespace folly {                             \
436   template <class T1, class T2>                 \
437   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) \
438   }
439 // Use this macro ONLY at global level (no namespace)
440 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(...)     \
441   namespace folly {                                 \
442   template <class T1, class T2, class T3>           \
443   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) \
444   }
445 // Use this macro ONLY at global level (no namespace)
446 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(...)         \
447   namespace folly {                                     \
448   template <class T1, class T2, class T3, class T4>     \
449   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) \
450   }
451
452 /**
453  * Instantiate FOLLY_ASSUME_FBVECTOR_COMPATIBLE for a few types. It is
454  * safe to assume that pair is compatible if both of its components
455  * are. Furthermore, all STL containers can be assumed to comply,
456  * although that is not guaranteed by the standard.
457  */
458
459 FOLLY_NAMESPACE_STD_BEGIN
460
461 template <class T, class U>
462   struct pair;
463 #ifndef _GLIBCXX_USE_FB
464 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
465 template <class T, class R, class A>
466   class basic_string;
467 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
468 #else
469 template <class T, class R, class A, class S>
470   class basic_string;
471 #endif
472 template <class T, class A>
473   class vector;
474 template <class T, class A>
475   class deque;
476 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
477 template <class T, class A>
478   class list;
479 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
480 template <class T, class C, class A>
481   class set;
482 template <class K, class V, class C, class A>
483   class map;
484 template <class T>
485   class shared_ptr;
486
487 FOLLY_NAMESPACE_STD_END
488
489 namespace folly {
490
491 // STL commonly-used types
492 template <class T, class U>
493 struct IsRelocatable< std::pair<T, U> >
494     : std::integral_constant<bool,
495         IsRelocatable<T>::value &&
496         IsRelocatable<U>::value> {};
497
498 // Is T one of T1, T2, ..., Tn?
499 template <class T, class... Ts>
500 struct IsOneOf {
501   enum { value = false };
502 };
503
504 template <class T, class T1, class... Ts>
505 struct IsOneOf<T, T1, Ts...> {
506   enum { value = std::is_same<T, T1>::value || IsOneOf<T, Ts...>::value };
507 };
508
509 /*
510  * Complementary type traits for integral comparisons.
511  *
512  * For instance, `if(x < 0)` yields an error in clang for unsigned types
513  *  when -Werror is used due to -Wtautological-compare
514  *
515  *
516  * @author: Marcelo Juchem <marcelo@fb.com>
517  */
518
519 namespace detail {
520
521 template <typename T, bool>
522 struct is_negative_impl {
523   constexpr static bool check(T x) { return x < 0; }
524 };
525
526 template <typename T>
527 struct is_negative_impl<T, false> {
528   constexpr static bool check(T) { return false; }
529 };
530
531 // folly::to integral specializations can end up generating code
532 // inside what are really static ifs (not executed because of the templated
533 // types) that violate -Wsign-compare and/or -Wbool-compare so suppress them
534 // in order to not prevent all calling code from using it.
535 FOLLY_PUSH_WARNING
536 FOLLY_GCC_DISABLE_WARNING(sign-compare)
537 #if __GNUC_PREREQ(5, 0)
538 FOLLY_GCC_DISABLE_WARNING(bool-compare)
539 #endif
540 FOLLY_MSVC_DISABLE_WARNING(4388) // sign-compare
541 FOLLY_MSVC_DISABLE_WARNING(4804) // bool-compare
542
543 template <typename RHS, RHS rhs, typename LHS>
544 bool less_than_impl(LHS const lhs) {
545   return
546     rhs > std::numeric_limits<LHS>::max() ? true :
547     rhs <= std::numeric_limits<LHS>::min() ? false :
548     lhs < rhs;
549 }
550
551 template <typename RHS, RHS rhs, typename LHS>
552 bool greater_than_impl(LHS const lhs) {
553   return
554     rhs > std::numeric_limits<LHS>::max() ? false :
555     rhs < std::numeric_limits<LHS>::min() ? true :
556     lhs > rhs;
557 }
558
559 FOLLY_POP_WARNING
560
561 } // namespace detail {
562
563 // same as `x < 0`
564 template <typename T>
565 constexpr bool is_negative(T x) {
566   return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x);
567 }
568
569 // same as `x <= 0`
570 template <typename T>
571 constexpr bool is_non_positive(T x) { return !x || folly::is_negative(x); }
572
573 // same as `x > 0`
574 template <typename T>
575 constexpr bool is_positive(T x) { return !is_non_positive(x); }
576
577 // same as `x >= 0`
578 template <typename T>
579 constexpr bool is_non_negative(T x) {
580   return !x || is_positive(x);
581 }
582
583 template <typename RHS, RHS rhs, typename LHS>
584 bool less_than(LHS const lhs) {
585   return detail::less_than_impl<
586     RHS, rhs, typename std::remove_reference<LHS>::type
587   >(lhs);
588 }
589
590 template <typename RHS, RHS rhs, typename LHS>
591 bool greater_than(LHS const lhs) {
592   return detail::greater_than_impl<
593     RHS, rhs, typename std::remove_reference<LHS>::type
594   >(lhs);
595 }
596
597 namespace traits_detail {
598 struct InPlaceTag {};
599 template <class>
600 struct InPlaceTypeTag {};
601 template <std::size_t>
602 struct InPlaceIndexTag {};
603 }
604
605 /**
606  * Like std::piecewise_construct, a tag type & instance used for in-place
607  * construction of non-movable contained types, e.g. by Synchronized.
608  * Follows the naming and design of std::in_place suggested in
609  * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0032r2.pdf
610  */
611 using in_place_t = traits_detail::InPlaceTag (&)(traits_detail::InPlaceTag);
612
613 template <class T>
614 using in_place_type_t =
615     traits_detail::InPlaceTypeTag<T> (&)(traits_detail::InPlaceTypeTag<T>);
616
617 template <std::size_t I>
618 using in_place_index_t =
619     traits_detail::InPlaceIndexTag<I> (&)(traits_detail::InPlaceIndexTag<I>);
620
621 inline traits_detail::InPlaceTag in_place(traits_detail::InPlaceTag = {}) {
622   return {};
623 }
624
625 template <class T>
626 inline traits_detail::InPlaceTypeTag<T> in_place(
627     traits_detail::InPlaceTypeTag<T> = {}) {
628   return {};
629 }
630
631 template <std::size_t I>
632 inline traits_detail::InPlaceIndexTag<I> in_place(
633     traits_detail::InPlaceIndexTag<I> = {}) {
634   return {};
635 }
636
637 // For backwards compatibility:
638 using construct_in_place_t = in_place_t;
639
640 inline traits_detail::InPlaceTag construct_in_place(
641     traits_detail::InPlaceTag = {}) {
642   return {};
643 }
644
645 /**
646  * Initializer lists are a powerful compile time syntax introduced in C++11
647  * but due to their often conflicting syntax they are not used by APIs for
648  * construction.
649  *
650  * Further standard conforming compilers *strongly* favor an
651  * std::initalizer_list overload for construction if one exists.  The
652  * following is a simple tag used to disambiguate construction with
653  * initializer lists and regular uniform initialization.
654  *
655  * For example consider the following case
656  *
657  *  class Something {
658  *  public:
659  *    explicit Something(int);
660  *    Something(std::intiializer_list<int>);
661  *
662  *    operator int();
663  *  };
664  *
665  *  ...
666  *  Something something{1}; // SURPRISE!!
667  *
668  * The last call to instantiate the Something object will go to the
669  * initializer_list overload.  Which may be surprising to users.
670  *
671  * If however this tag was used to disambiguate such construction it would be
672  * easy for users to see which construction overload their code was referring
673  * to.  For example
674  *
675  *  class Something {
676  *  public:
677  *    explicit Something(int);
678  *    Something(folly::initlist_construct_t, std::initializer_list<int>);
679  *
680  *    operator int();
681  *  };
682  *
683  *  ...
684  *  Something something_one{1}; // not the initializer_list overload
685  *  Something something_two{folly::initlist_construct, {1}}; // correct
686  */
687 struct initlist_construct_t {};
688 constexpr initlist_construct_t initlist_construct{};
689
690 } // namespace folly
691
692 // Assume nothing when compiling with MSVC.
693 #ifndef _MSC_VER
694 // gcc-5.0 changed string's implementation in libstdc++ to be non-relocatable
695 #if !_GLIBCXX_USE_CXX11_ABI
696 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string)
697 #endif
698 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector)
699 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::list)
700 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque)
701 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr)
702 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr)
703 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function)
704 #endif
705
706 /* Some combinations of compilers and C++ libraries make __int128 and
707  * unsigned __int128 available but do not correctly define their standard type
708  * traits.
709  *
710  * If FOLLY_SUPPLY_MISSING_INT128_TRAITS is defined, we define these traits
711  * here.
712  *
713  * @author: Phil Willoughby <philwill@fb.com>
714  */
715 #if FOLLY_SUPPLY_MISSING_INT128_TRAITS
716 FOLLY_NAMESPACE_STD_BEGIN
717 template <>
718 struct is_arithmetic<__int128> : ::std::true_type {};
719 template <>
720 struct is_arithmetic<unsigned __int128> : ::std::true_type {};
721 template <>
722 struct is_integral<__int128> : ::std::true_type {};
723 template <>
724 struct is_integral<unsigned __int128> : ::std::true_type {};
725 template <>
726 struct make_unsigned<__int128> {
727   typedef unsigned __int128 type;
728 };
729 template <>
730 struct make_signed<__int128> {
731   typedef __int128 type;
732 };
733 template <>
734 struct make_unsigned<unsigned __int128> {
735   typedef unsigned __int128 type;
736 };
737 template <>
738 struct make_signed<unsigned __int128> {
739   typedef __int128 type;
740 };
741 template <>
742 struct is_signed<__int128> : ::std::true_type {};
743 template <>
744 struct is_unsigned<unsigned __int128> : ::std::true_type {};
745 FOLLY_NAMESPACE_STD_END
746 #endif // FOLLY_SUPPLY_MISSING_INT128_TRAITS