Move the traits factories up, and remove <boost/mpl/has_xxx.hpp>
[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
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*);               \
46     template <typename>                                                        \
47     static std::false_type test(...);                                          \
48   };                                                                           \
49   template <typename TTheClass_>                                               \
50   using classname = decltype(                                                  \
51       classname##__folly_traits_impl__<TTheClass_>::template test<TTheClass_>( \
52           nullptr))
53
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__<                                    \
57       TTheClass_,                                                             \
58       RTheReturn_(TTheArgs_...) cv_qual> {                                    \
59     template <                                                                \
60         typename UTheClass_,                                                  \
61         RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual>                    \
62     struct sfinae {};                                                         \
63     template <typename UTheClass_>                                            \
64     static std::true_type test(sfinae<UTheClass_, &UTheClass_::func_name>*);  \
65     template <typename>                                                       \
66     static std::false_type test(...);                                         \
67   }
68
69 /*
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.
74  *
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
78  * function exists.
79  *
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.
82  *
83  * Say you need to know if a given class has a member function named
84  * `test` with the following signature:
85  *
86  *    int test() const;
87  *
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:
90  *
91  * namespace {
92  *
93  * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
94  *
95  * } // unnamed-namespace
96  *
97  * void some_func() {
98  *   cout << "Does class Foo have a member int test() const? "
99  *     << boolalpha << has_test_traits<Foo, int() const>::value;
100  * }
101  *
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
104  * is the same:
105  *
106  * void some_func() {
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;
113  * }
114  *
115  * @author: Marcelo Juchem <marcelo@fb.com>
116  */
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_>                     \
127   using classname =                                                           \
128       decltype(classname##__folly_traits_impl__<TTheClass_, TTheSignature_>:: \
129                    template test<TTheClass_>(nullptr))
130
131 namespace folly {
132
133 /**
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
139  * equivalent:
140  *
141  * void move1(T * from, T * to) {
142  *   new(to) T(from);
143  *   (*from).~T();
144  * }
145  *
146  * void move2(T * from, T * to) {
147  *   memcpy(to, from, sizeof(T));
148  * }
149  *
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.
154  *
155  * This property can be used in a variety of optimizations. Currently
156  * fbvector uses this property intensively.
157  *
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.
163  *
164  * You may also declare a type to be relocatable by including
165  *    `typedef std::true_type IsRelocatable;`
166  * in the class header.
167  *
168  * It may be unset in a base class by overriding the typedef to false_type.
169  */
170 /*
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.
174  */
175 /*
176  * IsZeroInitializable describes the property that default construction is the
177  * same as memset(dst, 0, sizeof(T)).
178  */
179
180 namespace traits_detail {
181
182 #define FOLLY_HAS_TRUE_XXX(name)                                             \
183   FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_##name, name);                     \
184   template <class T>                                                         \
185   struct name##_is_true : std::is_same<typename T::name, std::true_type> {}; \
186   template <class T>                                                         \
187   struct has_true_##name : std::conditional<                                 \
188                                has_##name<T>::value,                         \
189                                name##_is_true<T>,                            \
190                                std::false_type>::type {};
191
192 FOLLY_HAS_TRUE_XXX(IsRelocatable)
193 FOLLY_HAS_TRUE_XXX(IsZeroInitializable)
194 FOLLY_HAS_TRUE_XXX(IsTriviallyCopyable)
195
196 #undef FOLLY_HAS_TRUE_XXX
197
198 // Older versions of libstdc++ do not provide std::is_trivially_copyable
199 #if defined(__clang__) && !defined(_LIBCPP_VERSION)
200 template <class T>
201 struct is_trivially_copyable
202     : std::integral_constant<bool, __is_trivially_copyable(T)> {};
203 #elif defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
204 template <class T>
205 struct is_trivially_copyable : std::is_trivial<T> {};
206 #else
207 template <class T>
208 using is_trivially_copyable = std::is_trivially_copyable<T>;
209 #endif
210 }
211
212 struct Ignore {
213   template <class T>
214   /* implicit */ Ignore(const T&) {}
215   template <class T>
216   const Ignore& operator=(T const&) const { return *this; }
217 };
218
219 template <class...>
220 using Ignored = Ignore;
221
222 namespace traits_detail_IsEqualityComparable {
223 Ignore operator==(Ignore, Ignore);
224
225 template <class T, class U = T>
226 struct IsEqualityComparable
227     : std::is_convertible<
228           decltype(std::declval<T>() == std::declval<U>()),
229           bool
230       > {};
231 }
232
233 /* using override */ using traits_detail_IsEqualityComparable::
234     IsEqualityComparable;
235
236 namespace traits_detail_IsLessThanComparable {
237 Ignore operator<(Ignore, Ignore);
238
239 template <class T, class U = T>
240 struct IsLessThanComparable
241     : std::is_convertible<
242           decltype(std::declval<T>() < std::declval<U>()),
243           bool
244       > {};
245 }
246
247 /* using override */ using traits_detail_IsLessThanComparable::
248     IsLessThanComparable;
249
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>;
256 #else
257 /* using override */ using std::swap;
258
259 template <class T>
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&>()))
264       > {};
265 #endif
266 }
267
268 /* using override */ using traits_detail_IsNothrowSwappable::IsNothrowSwappable;
269
270 template <class T> struct IsTriviallyCopyable
271   : std::conditional<
272       traits_detail::has_IsTriviallyCopyable<T>::value,
273       traits_detail::has_true_IsTriviallyCopyable<T>,
274       traits_detail::is_trivially_copyable<T>
275     >::type {};
276
277 template <class T> struct IsRelocatable
278   : std::conditional<
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>
284     >::type {};
285
286 template <class T> struct IsZeroInitializable
287   : std::conditional<
288       traits_detail::has_IsZeroInitializable<T>::value,
289       traits_detail::has_true_IsZeroInitializable<T>,
290       std::integral_constant<bool, !std::is_class<T>::value>
291     >::type {};
292
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 {};
300
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 {};
308
309 template <typename T>
310 struct Negation : std::integral_constant<bool, !T::value> {};
311
312 template <bool... Bs>
313 struct Bools {
314   using valid_type = bool;
315   static constexpr std::size_t size() {
316     return sizeof...(Bs);
317   }
318 };
319
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...>>;
324
325 } // namespace folly
326
327 /**
328  * Use this macro ONLY inside namespace folly. When using it with a
329  * regular type, use it like this:
330  *
331  * // Make sure you're at namespace ::folly scope
332  * template<> FOLLY_ASSUME_RELOCATABLE(MyType)
333  *
334  * When using it with a template type, use it like this:
335  *
336  * // Make sure you're at namespace ::folly scope
337  * template<class T1, class T2>
338  * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
339  */
340 #define FOLLY_ASSUME_RELOCATABLE(...) \
341   struct IsRelocatable<  __VA_ARGS__ > : std::true_type {};
342
343 /**
344  * Use this macro ONLY inside namespace boost. When using it with a
345  * regular type, use it like this:
346  *
347  * // Make sure you're at namespace ::boost scope
348  * template<> FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType)
349  *
350  * When using it with a template type, use it like this:
351  *
352  * // Make sure you're at namespace ::boost scope
353  * template<class T1, class T2>
354  * FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(MyType<T1, T2>)
355  */
356 #define FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(...) \
357   struct has_nothrow_constructor<  __VA_ARGS__ > : ::boost::true_type {};
358
359 /**
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.
367  *
368  * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
369  *
370  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
371  *
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:
377  *
378  * template <class T1, class T2> class MyType { ... };
379  * ...
380  * // Make sure you're at global scope
381  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
382  */
383
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__) }   \
387   namespace boost { \
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(...)                         \
391   namespace folly {                                                     \
392   template <class T1> FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>) }       \
393     namespace boost {                                                   \
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(...)                 \
397   namespace folly {                                             \
398   template <class T1, class T2>                                 \
399   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) }               \
400     namespace boost {                                           \
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(...)                         \
405   namespace folly {                                                     \
406   template <class T1, class T2, class T3>                               \
407   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) }                   \
408     namespace boost {                                                   \
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(...)                         \
413   namespace folly {                                                     \
414   template <class T1, class T2, class T3, class T4>                     \
415   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) }               \
416     namespace boost {                                                   \
417     template <class T1, class T2, class T3, class T4>                   \
418     FOLLY_ASSUME_HAS_NOTHROW_CONSTRUCTOR(__VA_ARGS__<T1, T2, T3, T4>) }
419
420 /**
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.
425  */
426
427 FOLLY_NAMESPACE_STD_BEGIN
428
429 template <class T, class U>
430   struct pair;
431 #ifndef _GLIBCXX_USE_FB
432 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
433 template <class T, class R, class A>
434   class basic_string;
435 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
436 #else
437 template <class T, class R, class A, class S>
438   class basic_string;
439 #endif
440 template <class T, class A>
441   class vector;
442 template <class T, class A>
443   class deque;
444 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
445 template <class T, class A>
446   class list;
447 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
448 template <class T, class C, class A>
449   class set;
450 template <class K, class V, class C, class A>
451   class map;
452 template <class T>
453   class shared_ptr;
454
455 FOLLY_NAMESPACE_STD_END
456
457 namespace boost {
458
459 template <class T> class shared_ptr;
460
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> {};
466
467 } // namespace boost
468
469 namespace folly {
470
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> {};
477
478 // Is T one of T1, T2, ..., Tn?
479 template <class T, class... Ts>
480 struct IsOneOf {
481   enum { value = false };
482 };
483
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 };
487 };
488
489 /*
490  * Complementary type traits for integral comparisons.
491  *
492  * For instance, `if(x < 0)` yields an error in clang for unsigned types
493  *  when -Werror is used due to -Wtautological-compare
494  *
495  *
496  * @author: Marcelo Juchem <marcelo@fb.com>
497  */
498
499 namespace detail {
500
501 template <typename T, bool>
502 struct is_negative_impl {
503   constexpr static bool check(T x) { return x < 0; }
504 };
505
506 template <typename T>
507 struct is_negative_impl<T, false> {
508   constexpr static bool check(T) { return false; }
509 };
510
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.
515 FOLLY_PUSH_WARNING
516 FOLLY_GCC_DISABLE_WARNING(sign-compare)
517 #if __GNUC_PREREQ(5, 0)
518 FOLLY_GCC_DISABLE_WARNING(bool-compare)
519 #endif
520 FOLLY_MSVC_DISABLE_WARNING(4388) // sign-compare
521 FOLLY_MSVC_DISABLE_WARNING(4804) // bool-compare
522
523 template <typename RHS, RHS rhs, typename LHS>
524 bool less_than_impl(LHS const lhs) {
525   return
526     rhs > std::numeric_limits<LHS>::max() ? true :
527     rhs <= std::numeric_limits<LHS>::min() ? false :
528     lhs < rhs;
529 }
530
531 template <typename RHS, RHS rhs, typename LHS>
532 bool greater_than_impl(LHS const lhs) {
533   return
534     rhs > std::numeric_limits<LHS>::max() ? false :
535     rhs < std::numeric_limits<LHS>::min() ? true :
536     lhs > rhs;
537 }
538
539 FOLLY_POP_WARNING
540
541 } // namespace detail {
542
543 // same as `x < 0`
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);
547 }
548
549 // same as `x <= 0`
550 template <typename T>
551 constexpr bool is_non_positive(T x) { return !x || folly::is_negative(x); }
552
553 // same as `x > 0`
554 template <typename T>
555 constexpr bool is_positive(T x) { return !is_non_positive(x); }
556
557 // same as `x >= 0`
558 template <typename T>
559 constexpr bool is_non_negative(T x) {
560   return !x || is_positive(x);
561 }
562
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
567   >(lhs);
568 }
569
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
574   >(lhs);
575 }
576
577 namespace traits_detail {
578 struct InPlaceTag {};
579 template <class>
580 struct InPlaceTypeTag {};
581 template <std::size_t>
582 struct InPlaceIndexTag {};
583 }
584
585 /**
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
590  */
591 using in_place_t = traits_detail::InPlaceTag (&)(traits_detail::InPlaceTag);
592
593 template <class T>
594 using in_place_type_t =
595     traits_detail::InPlaceTypeTag<T> (&)(traits_detail::InPlaceTypeTag<T>);
596
597 template <std::size_t I>
598 using in_place_index_t =
599     traits_detail::InPlaceIndexTag<I> (&)(traits_detail::InPlaceIndexTag<I>);
600
601 inline traits_detail::InPlaceTag in_place(traits_detail::InPlaceTag = {}) {
602   return {};
603 }
604
605 template <class T>
606 inline traits_detail::InPlaceTypeTag<T> in_place(
607     traits_detail::InPlaceTypeTag<T> = {}) {
608   return {};
609 }
610
611 template <std::size_t I>
612 inline traits_detail::InPlaceIndexTag<I> in_place(
613     traits_detail::InPlaceIndexTag<I> = {}) {
614   return {};
615 }
616
617 // For backwards compatibility:
618 using construct_in_place_t = in_place_t;
619
620 inline traits_detail::InPlaceTag construct_in_place(
621     traits_detail::InPlaceTag = {}) {
622   return {};
623 }
624
625 /**
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
628  * construction.
629  *
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.
634  *
635  * For example consider the following case
636  *
637  *  class Something {
638  *  public:
639  *    explicit Something(int);
640  *    Something(std::intiializer_list<int>);
641  *
642  *    operator int();
643  *  };
644  *
645  *  ...
646  *  Something something{1}; // SURPRISE!!
647  *
648  * The last call to instantiate the Something object will go to the
649  * initializer_list overload.  Which may be surprising to users.
650  *
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
653  * to.  For example
654  *
655  *  class Something {
656  *  public:
657  *    explicit Something(int);
658  *    Something(folly::initlist_construct_t, std::initializer_list<int>);
659  *
660  *    operator int();
661  *  };
662  *
663  *  ...
664  *  Something something_one{1}; // not the initializer_list overload
665  *  Something something_two{folly::initlist_construct, {1}}; // correct
666  */
667 struct initlist_construct_t {};
668 constexpr initlist_construct_t initlist_construct{};
669
670 } // namespace folly
671
672 // Assume nothing when compiling with MSVC.
673 #ifndef _MSC_VER
674 // gcc-5.0 changed string's implementation in libgcc to be non-relocatable
675 #if __GNUC__ < 5
676 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string)
677 #endif
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)
684
685 // Boost
686 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(boost::shared_ptr)
687 #endif
688
689 /* Some combinations of compilers and C++ libraries make __int128 and
690  * unsigned __int128 available but do not correctly define their standard type
691  * traits.
692  *
693  * If FOLLY_SUPPLY_MISSING_INT128_TRAITS is defined, we define these traits
694  * here.
695  *
696  * @author: Phil Willoughby <philwill@fb.com>
697  */
698 #if FOLLY_SUPPLY_MISSING_INT128_TRAITS
699 FOLLY_NAMESPACE_STD_BEGIN
700 template <>
701 struct is_arithmetic<__int128> : ::std::true_type {};
702 template <>
703 struct is_arithmetic<unsigned __int128> : ::std::true_type {};
704 template <>
705 struct is_integral<__int128> : ::std::true_type {};
706 template <>
707 struct is_integral<unsigned __int128> : ::std::true_type {};
708 template <>
709 struct make_unsigned<__int128> {
710   typedef unsigned __int128 type;
711 };
712 template <>
713 struct make_signed<__int128> {
714   typedef __int128 type;
715 };
716 template <>
717 struct make_unsigned<unsigned __int128> {
718   typedef unsigned __int128 type;
719 };
720 template <>
721 struct make_signed<unsigned __int128> {
722   typedef __int128 type;
723 };
724 template <>
725 struct is_signed<__int128> : ::std::true_type {};
726 template <>
727 struct is_unsigned<unsigned __int128> : ::std::true_type {};
728 FOLLY_NAMESPACE_STD_END
729 #endif // FOLLY_SUPPLY_MISSING_INT128_TRAITS